Easy Event Handling for Vue 1 & 2
vue-events is a Vue.js plugin designed to simplify global event handling within applications built with Vue 1.x or Vue 2.x. It extends the Vue prototype with a `$events` object, which acts as a global event bus, abstracting away the underlying Vue instance's `$emit`, `$on`, and `$off` methods. The package provides several aliases for these core methods, such as `fire`/`emit` for `$emit`, `listen`/`on` for `$on`, and `remove`/`off` for `$off`, aiming for a more semantic API. The current stable version is 3.1.0, published in 2017. This package is not compatible with Vue 3.x, as Vue 3 removed the core event emitter API (`$on`, `$off`, `$once`) that this plugin relies upon. As such, while it served its purpose for earlier Vue versions, it is no longer actively maintained or suitable for modern Vue 3 development.
Common errors
-
TypeError: Cannot read properties of undefined (reading '$events')
cause The `vue-events` plugin was not correctly installed via `Vue.use(VueEvents)` before creating Vue instances, or the import path for `VueEvents` was incorrect.fixEnsure `import VueEvents from 'vue-events'; Vue.use(VueEvents);` is executed *before* any Vue instances that attempt to use `$events` are created. Verify the import path is correct. -
Property '$events' does not exist on type 'Vue'.
cause Using TypeScript without proper type declaration merging for the `vue-events` plugin, or the plugin was not installed globally onto the Vue prototype.fixEnsure the plugin is installed with `Vue.use(VueEvents)`. If using TypeScript, you might need to add a declaration file to augment the `Vue` instance type, typically by declaring a module `vue-events` and adding `$events` to `Vue.prototype`. -
This plugin cannot be installed because it has already been installed.
cause Attempting to call `Vue.use(VueEvents)` multiple times in the application lifecycle.fix`Vue.use()` should typically be called only once at the application's entry point (e.g., `main.ts` or `main.js`). Remove redundant calls to `Vue.use(VueEvents)`.
Warnings
- breaking This package is fundamentally incompatible with Vue 3.x. Vue 3 removed the `$on`, `$off`, and `$once` instance methods from Vue instances, which `vue-events` relies upon for its global event bus functionality. Attempting to use `vue-events` in a Vue 3 application will result in runtime errors.
- deprecated The global event bus pattern, while convenient for simple cases, is largely discouraged in modern Vue 2.x and especially Vue 3.x applications. It can lead to hard-to-trace event flows, hidden dependencies, and maintainability issues as applications grow.
- gotcha The package provides multiple aliases for its core methods (`fire`, `emit`, `$emit` for emitting; `listen`, `on`, `$on` for listening; `remove`, `off`, `$off` for removing). While intended for flexibility, this can lead to inconsistent code styles within a team and confusion about which alias to use.
Install
-
npm install vue-events -
yarn add vue-events -
pnpm add vue-events
Imports
- VueEvents
const VueEvents = require('vue-events')import VueEvents from 'vue-events'
- Vue
import { Vue } from 'vue'import Vue from 'vue'
- $events
this.$emit('myEvent', payload) (when intending to use the global bus)this.$events.fire('myEvent', payload)
Quickstart
import Vue from 'vue';
import VueEvents from 'vue-events';
// Install the plugin
Vue.use(VueEvents);
// Create a root Vue instance
new Vue({
el: '#app',
data() {
return {
message: 'Hello from Vue Events!'
};
},
mounted() {
// Listen for an event
this.$events.on('exampleEvent', (data) => {
console.log('Received exampleEvent:', data);
this.message = `Event received: ${data.detail}`;
});
// Fire an event after a short delay
setTimeout(() => {
this.$events.fire('exampleEvent', { detail: 'Data from emitter!' });
}, 1000);
},
beforeDestroy() {
// It's good practice to remove listeners to prevent memory leaks
this.$events.off('exampleEvent');
},
template: `
<div>
<h1>Vue Events Demo</h1>
<p>{{ message }}</p>
<button @click="triggerAnotherEvent">Trigger Another Event</button>
</div>
`,
methods: {
triggerAnotherEvent() {
this.$events.emit('anotherEvent', { source: 'button' });
}
}
});
// A separate component or instance can also listen or emit
new Vue({
mounted() {
this.$events.listen('anotherEvent', (data) => {
console.log('Another event caught!', data);
});
}
}).$mount(document.createElement('div')); // Mount without an element to just make it active.