0

i am trying to send and render some data from a child component to another child component & both components are rendered using one main component , how can i pass the data between two child components (in my vue app)?

exg

I have two child components A & B , "B" has click add to cart click event and have data of cart items , & i have template for cart item in component "A"
2
  • The quickest way is to use a service (instance class) required in both components. If you have a big app use vuex. I dont suggest you to use event bus because if you reload the event construction component it will call the event twice or more. Commented Jun 5, 2020 at 14:40
  • Can you please share some tutorial , based on this ? Commented Jun 6, 2020 at 5:15

3 Answers 3

3

In this situation, as both components share the same parent, it's common to move whatever shared state you need into the parent component and pass to the children as props.

Where components don't shared a direct parent, you can use an event bus (for very small apps) or Vuex. Vuex lets you share state between components in your app without having to pass props down through multiple levels.

Sign up to request clarification or add additional context in comments.

4 Comments

Another option worth noting is provide & inject
Provide/inject are not recommended to be used in general application code (see the note in the API docs). As these are more advanced features, you also risk making your code less understandable to other developers.
I have two child components A & B , "B" has click add to cart click event and have data of cart items , & i have template for cart item in component "A"
Considering the complexity of your app, I would personally be using Vuex for this. Things like shopping carts are prime examples of functionality where you're going to want to display and update the cart data from multiple components throughout the app.
0

There are a lot of ways to achieve this. I am explaining with global even bus concept. Following is an example. Here, child A and child B are communicating through event bus

const eventBus = new Vue ()

Vue.component('ChildB',{
  template:`
    <div id="child-b">
      <h2>Child B</h2>
      <pre>data {{ this.$data }}</pre>
      <hr/>
    </div>`, 
  data() {
    return {
      score: 0
    }
  },
  created () {
    eventBus.$on('updatingScore', this.updateScore)  // 3.Listening
  },
  methods: {
    reRender() {
      this.$forceUpdate()
    },
    updateScore(newValue) {
      this.score = newValue
    }
  }
})

Vue.component('ChildA',{
  template:`
    <div id="child-a">
      <h2>Child A</h2>
      <pre>data {{ this.$data }}</pre>
      <hr/>
      <button @click="changeScore">Change Score</button>
      <span>Score: {{ score }}</span>
    </div>`,
  props: ["score"],
  methods: {
    changeScore() {
    this.score +=200;
      eventBus.$emit('updatingScore', this.score+ 200)
    }
  }
})

Vue.component('ParentA',{
  template:`
    <div id="parent-a">
      <h2>Parent A</h2>
      <pre>data {{ this.$data }}</pre> 
      <hr/>
      <child-a :score="score"/>
      <child-b/>
    </div>`,
  data() {
    return {
      score: 100
    }
  } 
})


Vue.component('GrandParent',{
  template:`
    <div id="grandparent">
      <h2>Grand Parent</h2>
      <pre>data {{ this.$data }}</pre>
      <hr/>
      <parent-a/>
    </div>`,
})

new Vue ({
	el: '#app',
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <grand-parent/>
</div>

16 Comments

[Vue warn]: Error in created hook: "ReferenceError: eventBus is not defined" Appears
did you created event bus? like this const eventBus = new Vue ()
Yup created , but still its not updating data onclick on child component A to B
I have two child components A & B , "B" has click add to cart click event and have data of cart items , & i have template for cart item in component "A"
exactly I gave an example. In the above example run code snippet. I am updating score on Child A by button click and receiving in child B.
|
0

Ideally, you should use Vuex as state management pattern. But if your application is very simple and you don't need to do those operations often, you can pass data in emit payload from child to parent, and then parent component should pass it to another child via props

4 Comments

Actuallt i have two child components A & B , "B" has click add to cart click event and have data of cart items , & i have template for cart item in component "A"
Ok, I've seen it. What is your problem?
i've code like - on add to cart , cart items are stored in a variable in component "A" , and cart template is in component "B" , i want to pass component data from "A" to "B"
Ok and what is wrong with eventBus? What is your actual problem?