vue事件总线封装

158 阅读1分钟
const install = function(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 // 由于放在原型上
  // 触发示例this.$bus.emit('事件名称',传过去的数据)
  // 接受示例this.$bus.on('事件名称',接受数据)
}
export default install