A event bus for Vue.js
JavaScript
Switch branches/tags
Clone or download
Latest commit b914ad3 Jan 25, 2018
Permalink
Failed to load latest commit information.
.circleci Use ES modules (#1) Jan 25, 2018
__tests__ Use ES modules (#1) Jan 25, 2018
dist Use ES modules (#1) Jan 25, 2018
src Use ES modules (#1) Jan 25, 2018
.editorconfig initial commit Nov 8, 2016
.eslintignore Use ES modules (#1) Jan 25, 2018
.eslintrc.js Use ES modules (#1) Jan 25, 2018
.gitignore add test May 23, 2017
LICENSE Initial commit Nov 8, 2016
README.md add test May 23, 2017
package.json Use ES modules (#1) Jan 25, 2018
rollup.config.js Use ES modules (#1) Jan 25, 2018
yarn.lock Use ES modules (#1) Jan 25, 2018

README.md

vue-bus Build Status Coverage Status Downloads Version License

A event bus for Vue.js, support both Vue 1.0 and 2.0. See Vue documentation for more detail.

Installation

You can install it via yarn or npm.

$ yarn add vue-bus
$ npm install vue-bus --save

When used with a module system, you must explicitly install the bus via Vue.use():

import Vue from 'vue';
import VueBus from 'vue-bus';

Vue.use(VueBus);

You don't need to do this when using global script tags.

Usage

Listen and clean

// ...
created() {
  this.$bus.on('add-todo', this.addTodo);
  this.$bus.once('once', () => console.log('This listener will only fire once'));
},
beforeDestroy() {
  this.$bus.off('add-todo', this.addTodo);
},
methods: {
  addTodo(newTodo) {
    this.todos.push(newTodo);
  }
}

Trigger

// ...
methods: {
  addTodo() {
    this.$bus.emit('add-todo', { text: this.newTodoText });
    this.$bus.emit('once');
    this.newTodoText = '';
  }
}

Another way to use vue-bus

// xxx.js
import Vue from 'vue';

Vue.bus.emit('someEvent');

Note: on once off emit are aliases for $on $once $off $emit. See the API for more detail.

License

MIT