1.0.4 • Published 4 years ago
darkfriend-vue-eventbus v1.0.4
Vue eventBus
Easy eventbus for vue.js.
How install
npm i darkfriend-vue-eventbus -S
How use
global init
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)
}
}
})
local init
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);
});
Methods
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 |