# vue-bus

> A event bus for Vue.js

Latest version **1.2.1** (published 2019-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-bus
pnpm add vue-bus
yarn add vue-bus
bun add vue-bus
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2019-03-23 |
| First published | 2016-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 309 |
| Author | Yang Mingshan |
| Maintainers | yangmingshan |
| Keywords | vue, events, bus |

## Links

- npm: https://www.npmjs.com/package/vue-bus
- Repository: https://github.com/yangmingshan/vue-bus
- Homepage: https://github.com/yangmingshan/vue-bus#readme
- Issues: https://github.com/yangmingshan/vue-bus/issues
- npm.io page: https://npm.io/package/vue-bus

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.2.1 (latest) — 2019-03-23
- 1.2.0 — 2018-11-24
- 1.1.0 — 2018-01-25
- 1.0.0 — 2017-05-23
- 0.3.0 — 2016-11-11
- 0.2.0 — 2016-11-09
- 0.1.2 — 2016-11-09
- 0.1.1 — 2016-11-08
- 0.1.0 — 2016-11-08

## README

# vue-bus [![Build Status](https://img.shields.io/circleci/project/yangmingshan/vue-bus.svg)](https://circleci.com/gh/yangmingshan/vue-bus) [![Coverage Status](https://img.shields.io/codecov/c/github/yangmingshan/vue-bus.svg)](https://codecov.io/gh/yangmingshan/vue-bus) [![Downloads](https://img.shields.io/npm/dt/vue-bus.svg)](https://www.npmjs.com/package/vue-bus) [![Version](https://img.shields.io/npm/v/vue-bus.svg)](https://www.npmjs.com/package/vue-bus) [![License](https://img.shields.io/npm/l/vue-bus.svg)](https://www.npmjs.com/package/vue-bus)

A event bus for Vue.js, support both Vue 1.0 and 2.0. See Vue [documentation](https://vuejs.org/v2/guide/migration.html#Events) for more detail.

## Installation
You can install it via [yarn](https://yarnpkg.com) or [npm](https://npmjs.com).
```
$ yarn add vue-bus
$ npm install vue-bus --save
```
And it's available on [jsdelivr](https://cdn.jsdelivr.net/npm/vue-bus/dist/vue-bus.js) or [unpkg](https://unpkg.com/vue-bus/dist/vue-bus.js).
```html
<!-- development version -->
<script src="https://cdn.jsdelivr.net/npm/vue-bus/dist/vue-bus.js"></script>

<!-- production version -->
<script src="https://cdn.jsdelivr.net/npm/vue-bus/dist/vue-bus.min.js"></script>
```
When used with a module system, you must explicitly install the bus via Vue.use():
```js
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
```js
// ...
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
```js
// ...
methods: {
  addTodo() {
    this.$bus.emit('add-todo', { text: this.newTodoText });
    this.$bus.emit('once');
    this.newTodoText = '';
  }
}
```
#### Another way to use vue-bus
```js
// 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](https://vuejs.org/v2/api/#Instance-Methods-Events) for more detail.*

## License
[MIT](https://opensource.org/licenses/MIT)

---
_Source: https://npm.io/package/vue-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
