An enhanced component of Vue event bus can be automatically destroyed without manual cancellation of subscription events, modularization, immutability of event data and many other features.
npm i --save vue-super-eventbus
import EventBus from "vue-super-eventbus"
Vue.use(EventBus)
// Recommend
on:{
event1(data){
// handle data
console.log(data)
},
event2(data){
// handle data
console.log(data)
}
},
// Or
created() {
this.$bus.on(this, "event1", data => {
// handle data
console.log(data);
});
this.$bus.on(this, "event2", data => {
// handle data
console.log(data);
});
},
created() {
this.$bus
.emit("event1", 'test message')
.emit("event2", { message: "test json message" });
}
beforeDestroy() {
// No need to off the event, The lib take care of this for you.
}