Easy eventbus for vue.js.
npm i darkfriend-vue-eventbus -S
import Vue from 'vue';
import eventBus from 'darkfriend-vue-eventbus';
Vue.use(eventBus);
new Vue({
mounted() {
// listen event
this.eventBus.$on('eventName', (eventData) => {
console.log(eventData); // {param1: 'value1', param2: 'value2'}
});
// send event with args
let args = {param1: 'value1', param2: 'value2'};
this.eventBus.$emit('eventName', args);
}
});
The use eventBus
component options.
new Vue({
eventBus: {
eventName(eventData){
console.log(eventData)
}
}
})
import {eventBus} from 'darkfriend-vue-eventbus';
// send
let args = {param1: 'value1', param2: 'value2'};
eventBus.$emit('eventName', args);
// listen
eventBus.$on('eventName', (eventData) => {
console.log(eventData);
});
Method | Params | Description | Docs |
---|---|---|---|
eventBus.$emit |
event, payload |
Emit the event with the given payload. | vm.$emit |
eventBus.$on |
event, callback |
Listen for the event with the given callback. | vm.$on |
eventBus.$off |
event, callback |
Remove event listener(s) for the event | vm.$off |
eventBus.$once |
event, callback |
Listen for a custom event, but only once | vm.$once |