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"
  • 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. – miorey Jun 05 '20 at 14:40
  • Can you please share some tutorial , based on this ? –  Jun 06 '20 at 05:15

3 Answers3

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.

Nilson Jacques
  • 468
  • 3
  • 8
  • Another option worth noting is [provide & inject](https://vuejs.org/v2/guide/components-edge-cases.html#Dependency-Injection) – Kamil Bęben Jun 05 '20 at 10:50
  • Provide/inject are not recommended to be used in general application code (see the [note in the API docs](https://vuejs.org/v2/api/#provide-inject)). As these are more advanced features, you also risk making your code less understandable to other developers. – Nilson Jacques Jun 05 '20 at 10:53
  • 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" –  Jun 05 '20 at 11:26
  • 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. – Nilson Jacques Jun 05 '20 at 13:31
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>
Muni Kumar Gundu
  • 512
  • 5
  • 11
  • [Vue warn]: Error in created hook: "ReferenceError: eventBus is not defined" Appears –  Jun 05 '20 at 11:17
  • did you created event bus? like this const eventBus = new Vue () – Muni Kumar Gundu Jun 05 '20 at 11:19
  • Yup created , but still its not updating data onclick on child component A to B –  Jun 05 '20 at 11:23
  • 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" –  Jun 05 '20 at 11:25
  • 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. – Muni Kumar Gundu Jun 05 '20 at 11:28
  • Share your code with codepen or any equivalent. I can guide you – Muni Kumar Gundu Jun 05 '20 at 11:39
  • your example have grand parent & parent , how i need to have that code also ? –  Jun 05 '20 at 11:39
  • i have two component files actually –  Jun 05 '20 at 11:40
  • Sure Just a minute –  Jun 05 '20 at 11:46
  • https://codesandbox.io/s/black-wave-h6j57?file=/src/views/Laptops.vue –  Jun 05 '20 at 11:47
  • Please take a look at this , i am trying to show cart items into cart on add to cart button –  Jun 05 '20 at 11:49
  • 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 –  Jun 05 '20 at 12:06
  • I updated the code. https://codesandbox.io/s/dreamy-sanderson-h5fop. But not validated. What you shared is throwing errors. Check and let me know – Muni Kumar Gundu Jun 05 '20 at 14:19
  • Hello mani , please take a look at this working codepen - https://codesandbox.io/s/competent-pine-8u9mt?file=/src/main.js –  Jun 06 '20 at 06:30
  • codesandbox.io/s/competent-pine-8u9mt?file=/src/main.js still not working. Did you tried the file I updated in codepen codesandbox.io/s/dreamy-sanderson-h5fop ? – Muni Kumar Gundu Jun 08 '20 at 14:39
  • Hello mani , this seems to be working codepen -https://codesandbox.io/s/competent-pine-8u9mt?file=/src/main.js , i did not found any changes in the pen that you've sent. –  Jun 09 '20 at 04:43
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

akim lyubchenko
  • 247
  • 4
  • 8
  • 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" –  Jun 05 '20 at 11:59
  • Ok, I've seen it. What is your problem? – akim lyubchenko Jun 05 '20 at 12:01
  • 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" –  Jun 05 '20 at 12:05
  • Ok and what is wrong with eventBus? What is your actual problem? – akim lyubchenko Jun 05 '20 at 12:12