Easy Event Handling for Vue 1 & 2

3.1.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `vue-events` and use its `$events.on` and `$events.fire` methods to communicate between parts of a Vue 2.x application.

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.

view raw JSON →