1

I have a child component that needs to say something to the parent of a parent (the grandparent?). Right now, this only works if the parent acts as a conduit:

from the child component, I call this.$emit('hello', 'hi grandma!'). Then the parent does the same from the sayHello method, and then the grandparent finally receives the data:

<grandparent>
  <parent @hello="sayHello">
    <child></child>
  </parent>
</grandparent>

is there a better way to communicate up the chain? It looks like the docs recommend an event hub; this strikes me as a more complicated solution?

Is there any reason I shouldn't use this.$parent.$emit('hello', 'hi grandma!')?

tony19
  • 125,647
  • 18
  • 229
  • 307
Justin
  • 3,418
  • 3
  • 27
  • 37
  • The docs *do* recommend a bus. Or you can use a state management solution. Or you can [roll your own](https://stackoverflow.com/q/42615445/38065). – Bert Jul 20 '17 at 16:34
  • thanks. i just edited my question - can `this.$parent.$emit('hello', 'hi grandma!')` be a solution here, or is that bad practice? – Justin Jul 20 '17 at 16:35
  • 2
    That is a bad practice. Generally you don't want your components reaching out of themselves. Using $parent works if its a grandchild, but what if its a great grandchild? – Bert Jul 20 '17 at 16:36
  • thanks for the help here - and yes, this is definitely a dupe. my ultimate solution here was to chain together my `$emit`s in a way that envisions my components as mini api-like objects. although it feels a bit repetitive, i think it ultimately makes the components more flexible. and i'm sure there is a good reason (performance?) for preventing endless emitting up the component tree. – Justin Jul 20 '17 at 17:04

0 Answers0