Vues
$emit
not very comprehensive, however there are several ways to do this. First, you need $emit
in the VUE model for which you want to send a message if you want to use this.$on()
, so if you are sending from a component, you can pass it to the direct parent using:
this.$parent.$emit('myEvent',true);
However, this can become problematic if you have a long chain of parents, because you have to $emit
create a child chain, so in this case you can use an instance of Vue as a bus:
Or if you are using separate file components.
window.bus = new Vue({});
Then in the receiver:
bus.$on('myEvent',() => { // Do stuff });
And in the emitter:
bus.$emit('myEvent',true);
Finally, if you find that the app
getting too complicated for a simple bus, you can use vuex
:
https://vuex.vuejs.org/en/index.html
source share