Invoke parent/host component method from a child/encapsulated component #10765
-
Hi. I am curious what are the Vue.js recommended way(s) to invoke a method of a parent/host component from a child/encapsulated component. The ways to do this, in the high level are, afaik :
But ... this article claims that #2 (or at least #2a) "... is something you shouldn't ever be doing in Vue ...". Any opinions on this? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Passing a function as a prop and passing a function as an event listener achieves pretty much the same. The difference is mainly this: When the child emits an event, it doesn't care wether there's a listener from the parent. In other words, evetn listeners are always optional. When the child calls a function passed through a prop, the child has to make sure the function actually exists, but on the flip-side, the child can actually work with the function's return value, whereas emitting an event does not return anything in the child Using events is the default pattern used in Vue. But passing a function as a prop can be useful sometimes, i.e. when you want to work with the function's return value in the child. |
Beta Was this translation helpful? Give feedback.
Passing a function as a prop and passing a function as an event listener achieves pretty much the same.
The difference is mainly this: When the child emits an event, it doesn't care wether there's a listener from the parent. In other words, evetn listeners are always optional. When the child calls a function passed through a prop, the child has to make sure the function actually exists, but on the flip-side, the child can actually work with the function's return value, whereas emitting an event does not return anything in the child
Using events is the default pattern used in Vue. But passing a function as a prop can be useful sometimes, i.e. when you want to work with the function's retur…