How can Vue JS 3 components exchange data between each other?

I am attempting to share data from a variable favorite_count in the Favorites component located in the file Favorites.vue. My goal is to pass this data to the App Component in the App.vue file but I have been unsuccessful so far. I want any changes made to the value of favorite_count in the Favorites component to also reflect in the App Component. Despite conducting thorough research online, I have not found a solution yet. Any suggestions on what could be causing the issue?

Favorites.vue file

<template>
    <div class="row m-5">
        <h3 class="col-8">Your Favorites</h3>
        <div class="col-4">
            <p class="pull-right m-1">Picks 
            <span >{{ favorite_count }}</span>  / 5</p>
        </div>
        <hr>
    </div>
</template>
<script>
export default {
    name: 'favorites',
    data() {
        return {
            favorite_count: 5,
        }
    },
    methods: {
        changeFavoriteCount() { 
            this.favorite_count = this.favorite_count + 2;
        },
        emitToParent (event) {
          this.$emit('childToParent', this.favorite_count)
        }
    }
}
</script>

App.vue file

<template>
    <div class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav"> 
                <li class="nav-item">
                    <router-link to="/favorites" class="btn btn-info">
                      Favorites ( {{ favorite_count }} )
                    </router-link>
                </li>
            </ul>
        </div>
    </div> 
</template>
<script> 
import Favorites from './components/Favorites.vue'
 
export default {
  name: 'App',
  components: {
    Favorites
  },
  data () {
    return { 
      favorite_count: 0, 
    }
  }
}
</script>

Answer №1

Incorporating <router-view> into your application at a later stage requires careful consideration. I recommend checking out this solution

If you plan on embedding Favorites within <template> in App.vue, utilizing props is advised:

1. Define the 'shared' variable in the parent component (App.vue)

data () {
  return { 
    favorite_count: 0, 
  }
},

2. Specify props in the child component (Favorites.vue)

export default {
  props: { favorite_count: Number },
  ...
}

3. Pass favorite_count as a prop to Favorites

<template>
  ...
    <Favorites :favorite_count="favorite_count" ... />
</template>

To update favorite_count, emit an event to the parent component. For further details, refer to the Vue docs

Edit: To clarify, if you intend to modify favorite_count from Favorites.vue, emitting an event to App.vue is necessary to prevent prop mutation.

This entails relocating the changeFavoriteCount() function to App.vue and setting up a listener in the child component to execute this function:

// App.vue
<template>
  ...
    <Favorites 
       @your-update-event="changeFavoriteCount" 
       :favorite_count="favorite_count" ...
    />
</template>

...

changeFavoriteCount(newVal) { 
    this.favorite_count = newVal;
},

Answer №2

modify your Favourite.vue file with the following changes

<template>
  <div class="row m-5">
    <h3 class="col-8">Your Top Choices</h3>
    <div class="col-4">
      <p class="pull-right m-1">
        Selections <span>{{ favorite_count }}</span> / 5

        <button @click="changeFavoriteCount"gt;Update preferred_count</button>
      </p>
    </div>
    <hr />
  </div>
</template>
<script>
export default {
  name: "topFavorites",
  data() {
    return {
      favorite_count: 5,
    };
  },
  methods: {
    changeFavoriteCount() {
      this.favorite_count = this.favorite_count + 2;
      this.emitToParent();
    },
    emitToParent() {
      this.$emit("childToParent", this.favorite_count);
    },
  },
};
</script>

adjust the App.vue file as shown below

<template>
    <div class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav"> 
                <li class="nav-item">
                    <router-link to="/favorites" class="btn btn-info">
                      
                      <Top Favorites @childToParent="updateFavorite" />
                      Top Choices ( {{ favorite_count }} )
                    </router-link>
                </li>
            </ul>
        </div>
    </div> 
</template>
<script> 
import TopFavorites from './components/TopFavorites.vue'
 
export default {
  name: 'App',
  components: {
    TopFavorites
  },
  data () {
    return { 
      favorite_count: 0, 
    }
  },
  methods: {
    updateFavorite(data) {
      this.favorite_count = data;
    },
  },
}
</script>

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

Guide on utilizing Nextjs middleware for directing users to sub-domain according to their IP address

I've been struggling to effectively implement NextJs's middleware for redirecting users from a specific country to another web domain. I've encountered some issues with my setup: Main domain: https://www.example.com sub-domain: src/middle ...

Merge information from various sources using ajax

Currently, I have a single ajax request that retrieves data from an API and uses it to generate a table. Now, I'm looking to modify the code so that it can retrieve data from two different URLs and merge them into the same table (retTable). Below is ...

Transferring HTML information to Flask

I'm struggling with passing a value from a text box in a web application to a Flask application. Despite my efforts, the request object in Flask does not seem to contain the data I need. Can anyone provide assistance with this issue? Below are the rel ...

Unit testing controllers in AngularJS with Karma often involves setting up mock services to simulate dependencies

Currently, I am immersed in the development of a Single Page Application using AngularJS as part of my Treehouse Full Stack JavaScript TechDegree. My main focus right now is on conducting unit tests for the controllers. The challenge lies in testing contro ...

Manipulating classes within ng-class in AngularChanging classes in ng-class dynamically

Featuring multiple elements with an ng-class that behaves similarly to a ternary operator: ng-class="$ctrl.something ? 'fa-minus' : 'fa-plus'" To access these elements, we can compile all the ones with fa-minus and store them in a lis ...

Which is more efficient for rendering performance: using images, CSS gradients, or box shadows with borders?

I'm curious about improving website scroll and animation performance. Which option would be better for your mobile webapp or website: Using a repeating thin image or CSS3 gradient? or Utilizing a repeating image instead of box shadow with a borde ...

Refreshing Angular Page

I'm looking for a way to reset my Angular page back to its original state with just one button click. I attempted to use the angular.copy method, but encountered an error. I have various scope and controller variables that I don't want to reset i ...

The issue with style.background not functioning in Internet Explorer is causing complications

I am currently developing a JavaScript game that involves flipping cards. However, I have encountered an issue with the style.background property. It seems to be functioning correctly in Chrome but not in Internet Explorer. Here is the problematic code sn ...

Consolidate two AJAX requests into a single callback

I'm developing a Chrome extension and facing the challenge of merging two separate AJAX calls into one callback upon success. What would be the most efficient approach to achieve this? Auth.prototype.updateContact = function(id, contact_obj) { var ...

Navigate the array and divide the elements into separate values

For a project I need to upload files, wherein the data is organized within an object like [5.76516834507, 50.8474898368], [5.76115833641, 50.8453698247]. The task here is to extract and store the first value as latitude: 5.76516834507 and the second value ...

"Integrating multiple partials with templateUrl in AngularJS: A step-by-step

Is there a way to merge partial views using the templateUrl attribute in an AngularJS directive? Imagine having a directive like this: .directive('combinePartials', function () { var mainPartial = "mainpartial.html", var template2 = "pa ...

Consistently scaling the Embla carousel slides for a seamless presentation experience

In my current project, I am utilizing Embla Carousels and aiming to incorporate a smooth slide scaling effect as users scroll through. The idea is for slides to enlarge the closer they get to the left edge of the carousel container, then decrease in size r ...

"Unexpected Type Inference Issue: A variable initially defined as a string inexplicably transforms into 'undefined'

Currently, I am incorporating the await-to-js library for handling errors (specifically utilizing the to method from the library). In an intriguing scenario, the variable type shifts to string | undefined within a for..of loop, whereas outside of the loop ...

Have you ever encountered the orientationchange event in JavaScript before?

When does the orientationchange event trigger in relation to window rotation completion? Is there a way to fire an event before the operating system initiates the integrated window rotation? Edit: For example, can elements be faded out before the rotation ...

Unable to add the div using a jQuery click event

Hey there, I'm looking to add a div dynamically when clicking on another div. Check out the code snippet below: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title></title> <script src= ...

A step-by-step guide on transferring Data URI from canvas to Google Sheet using the fetch method

I am trying to send an image as base64 code to a Google sheet using fetch. However, I am encountering an error that says "data_sent is not defined" when I run my code. I need help identifying the problem and finding a solution to fix it. For reference, & ...

Retrieve the location of the selected element

We are faced with the challenge of determining the position of the button clicked in Angular. Do you think this is achievable? Our current setup involves an ng-grid, where each row contains a button in column 1. When the button is clicked, we aim to displ ...

Exploring methods to test the use of custom CSS variables in VueJS

I am currently working on a VUEJS test and I need to access the style information. The component is being utilized in this manner: :style="background-color: var(--color)" The following methods are being used: wrapper.find('.avatar'). ...

Angular.js and D3 - The Perfect Combination for Dynamic Data Visualization!

Having some trouble creating a chart with angular.js. The chart is not appearing on the page when using rout.js, but it works fine without it. Here's my code: var myapp = angular.module('myapp', ['angularCharts']); function D3 ...

I am receiving a success message from webpack indicating that the compilation was successful, but I am encountering a blank page in the

My app.js looks like this: import Home from "./pages/home/Home"; import Login from "./pages/login/Login"; import Profile from "./pages/profile/Profile"; import Register from "./pages/register/Register"; import { Brow ...