0.0.4 • Published 2 years ago

@wholezb/plugin-vue-handler v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

plugin-vue-handler

npm version

A plugin that supports Vue to dispatch global events.

Setup

install:

npm i @wholezb/plugin-vue-handler -S

You need to add configuration for vue-cli to correctly translate the es module in node_modules:

// vue.config.js:
module.exports = {
    transpileDependencies: [
      '@wholezb/*' // all of node_module for '@wholezb'
    ]
}

register vue plugin:

import Handler from '@wholezb/plugin-vue-handler';

// bind Vue.emit()
// bind this.$handler.on(type, handler, component = null)
// bind this.$handler.off(type, component = null)
// bind this.$handler.emit(type, option = null, component = null)

Vue.use(Handler);

Usage

// app.vue or any children:
export default {
  name: 'app',

  handler: {
    ['global-event-name'](option) {
      // this = the VueComponent
      this.methodTest(option);
    }
  },

  methods: {
    methodTest(option) {
      console.warn('test-event.app:', this, option);
    }
  }
}

// Then call the following code in any business code
// to dispatch a global event so that any subcomponents
// that listen to this event "global-event-name" respond to the event:
Vue.emit('global-event-name', { arguments: 123 });