Passing data from one component to another in Vue: A database perspective

I'm struggling to pass the id of a clicked button from MovieTable.vue to the WatchedForm.vue component. The WatchedForm.vue component is responsible for updating the data in the database based on the provided id, which corresponds to the Movie_id in the database. I attempted to use props to achieve this, but unfortunately couldn't get it to work. Any assistance would be greatly appreciated as I'm feeling quite frustrated!

App.vue:


        <template>
          <div class="container p-5">
            <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#movieModal">
              Add Movie
            </button>
        
            <movie-form @add:movie="addMovie" />
            <movie-table
                :movies="movies"
                @delete:movie="deleteMovie"
                @edit:movie="WatchedMovie"
                @edit2:movie="unWatchedMovie"
            />
          </div>
          
          <watched-form
                @edit:movie="watchedMovie"
                @edit2:movie="unWatchedMovie"
                />
        </template>
    

MovieTable.vue:


        <template>
          <div id="movie-table">
            <p v-if="movies.length < 1" class="empty-table">No movies</p>
            <table v-else>
              <thead>
              ...
            </table>
          </div>
        </template>
    

And WatchedForm.vue:


        <template>
          <div id="watched-form">
            <div class="modal fade" id="watchedModal" tabindex="-1" aria-labelledby="watchedModalLabel" aria-hidden="true">
                ...
            </div>
          </div>
        </template>
    

Answer №1

To implement the necessary changes in App.vue, make sure to pass the movie id as props from MovieTable.vue to WatchedForm.vue using a computed property instead of a variable. Here is the recommended method:

 <movie-table
      :movies="movies"
      @delete:movie="deleteMovie"
      @edit:movie="WatchedMovie"
      @edit2:movie="unWatchedMovie"
  />
  </div>
  <watched-form
      :movieId="getMovieId"
      />

Additionally, introduce a variable named movieId within the data() function:

data() {
 return {
  movieId: '',
  ....other variables
 };
}

Define a computed property as follows:

computed: {
 getMovieId() {
  return this.movieId;
 }
}

Next, update your methods accordingly to set the emitted movieId from MovieTable.vue in order for the computed property to transmit the updated value to WatchedForm.vue:

methods: {
    async watchedMovie(Movie_id, updatedMovie) {
      this.movieId = Movie_id; // Storing the movie_id locally to trigger the computed property and update WatchedForm.vue
      // ...Other lines of code
    },
    async unWatchedMovie(Movie_id, updatedMovie) {
      this.movieId = Movie_id; // Storing the movie_id locally to trigger the computed property and update WatchedForm.vue
      // ...Other lines of code
    }
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The negative z-index is causing issues with my ability to select classes using jQuery

By setting a z-index of -3 for several divs in my background, I thought they wouldn't affect the formatting of elements in the foreground. But now I'm facing an issue where I can't click on those background divs when trying to target them wi ...

Troubleshooting issue: Django and Javascript - Why is my dependent dropdown feature not

I am new to using a combination of Javascript and Django. Below is the script I have written: <script> $(document).ready(function() { $("#source").change(function() { var el = $(this); var reg = ...

Error encountered with Content Security Policy while utilizing react-stripe

I am currently in the process of developing a React application that incorporates Stripe payments utilizing the @stripe/react-stripe-js (version "^1.9.0") and @stripe/stripe-js (version "^1.32.0") npm packages. While the payments are functioning correctly, ...

What is causing these TypeScript type assertions to go unnoticed?

While reviewing type assertions, I noticed something interesting about the last three variable assignments - they don't produce errors. It's perplexing because I thought I was trying to change 'helo' into 'hello', which should ...

Detecting when the Ctrl key is pressed while the mouse is hovering over an element in React

In my project, I have implemented a simple Grid that allows users to drag and drop items. The functionality I am trying to achieve is detecting when the mouse is positioned on the draggable icon and the user presses the Ctrl key. When this happens, I want ...

Looping through an array of data retrieved from the MS Graph API

When using node to call the MS Graph API with v1.0/users, the returned data shows a list of users in the following format after doing a console.log(users): { [ { displayName: "bob dole" }, { displayName: "steve st ...

Is it possible to perform a GET request between a site using HTTPS and another using HTTP?

https://i.stack.imgur.com/AlWYJ.png I recently hosted my site on Shopify using HTTPS, and then I attempted to make a GET request via Angular to a site that uses HTTP. Angular <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.j ...

Remove a data entry from the MySQL database by selecting the corresponding row in the table and utilizing the DELETE function

Is there a way to remove a record from my MySQL database by clicking on a row in a table that is generated using ejs? Below is the code I am currently using to generate the table rows. <% res.forEach(function(item){ %> <tr> ...

Jest may come across test suites, but it discreetly disregards the individual tests

Having encountered an issue with Jest testing in a Nuxt/Vue v2 project, I found that after making some changes, the tests were no longer running. The unit tests were either passing or failing initially, but suddenly stopped running altogether. ----------|- ...

When using Python Selenium, we can see JavaScript in the view page source, but inspecting elements reveals the

I'm currently facing a challenge in accessing links to attachments for a web automation project. The issue lies in the fact that while I can view the HTML Code (divs and tables) when loading the webpage via Chrome and inspecting element, all I see in ...

Sending a JavaScript object as a prop in a React component

Currently, I am enrolled in a React course that requires us to pass a single JavaScript object as props to a React application. Here's the code snippet I have been working on: import React from 'react'; import ReactDOM from 'react-dom& ...

Utilize Vue and Django to submit dynamic form data efficiently

Currently utilizing Vue.js to send data from a standard .html form to Django. My experience with Python and Django is relatively fresh, only a few days in. Within my form, there is a segment where users can dynamically input form fields (both regular inpu ...

JavaScript issues in FireFox and Internet Explorer when using JQuery

I have developed a PHP GD image for generating captcha images, saved as image.php. Each time it creates a unique captcha image. Inside the signup.php file: <img alt="" src="image.php"> <input type="button" id="btn" value="cannot read captcha"&g ...

Issues with Promise execution sequence in ExpressJS Middleware

I need help creating an Express Middleware that checks if the user/password pair in the authorization header exists in a JSON file for educational purposes. I have integrated this middleware into a simple unit converter app. The issue I'm facing is t ...

Using PHP and JavaScript to keep track of file modifications

Being a beginner in PHP and Javascript, I recently created a basic Clock application using Javascript which worked out successfully. Now, I have a file that will automatically change its content. For example, it might look something like this: "12h15: eat ...

Is it possible to capture and store server responses on the client side using Node.js and React Native?

Here's a POST request I have: router.post("/projects", async (req, res) => { const { projectName, projectDescription, projectBudget, projectDuration, industry, companyName, numberOfEmployees, diamond, } = req.bod ...

Node.js - Error: JSON.Parse and JSON.Stringify are not recognized as functions

Is it possible to convert a string to JSON and vice versa without any additional npm packages? I am currently using JSON.Stringfy in my router.js file. Are there any specific npm modules that need to be added to the project in order to use the JSON.Stringf ...

How can we rearrange the positions of three items in an array?

I am dealing with a function that returns an array of objects structured like this const allGreen = _.filter( sidebarLinks, side => !isServicePage(side.slug.current) ); https://i.stack.imgur.com/dR8FL.png I am attempting to rearrange the positions of ...

Instructions for creating a slush machine

Today, I tried to create a slush generator but ran into some issues. Even after following tutorials online and double-checking my work, the generator doesn't seem to be functioning properly. If anyone can identify what I might have done wrong, please ...

JavaScript's Selenium WebDriver - Explicit Waiting

Currently, I am utilizing selenium-webdriverjs. My objective is to pause until a specific element is displayed. To accomplish this, I have implemented an explicit wait that operates effectively: var shown = false; driver.wait(function(){ driver.findEl ...