Vue中央事件总线插件

283 阅读1分钟

vue-bus.js代码

import Vue from 'vue'
const Bus = new Vue({
  methods: {
    emit(event, ...args) {
      this.$emit(event, ...args)
    },
    on(event, callback) {
      this.$on(event, callback)
    },
    off(event, callback) {
      this.$off(event, callback)
    }
  }
})
Vue.prototype.$bus = Bus

export default Bus

main.js引入

import Bus from './vue-bus.js'
Vue.use(Bus)