Vue组件之Bus事件总线

进行非父子组件之间的进行通信,

简单的场景:

1、使用一个空的vue实例作为中央事件总线

var bus = new Vue()

//  在 A 组件 中使用

bus.$emit('事件名',params)

//  在 B 组件 中使用

bus.$on('事件名',function(params){

})

//  清除 事件 

bus.$off('事件名')

如果在 vue 项目中全局引入

var eventBus = {
  install(Vue,options) {
    Vue.prototype.$bus = vue
  }
};
Vue.use(eventBus);

后续的调用就是  this.$bus.$on.......

posted @ 2020-10-23 09:15  xushenz  阅读(258)  评论(0)    收藏  举报