vue bus通信

const eventBus = {
    install(Vue,options) {
        Vue.prototype.$bus = vue
    }
};
Vue.use(eventBus);
View Code
methods: { //分发事件
  todo: function () {
    this.$bus.$emit('todoSth', params); 
  }
}
View Code
created() {
  this.$bus.$on('todoSth', (params) => {  
      //todo something
  })
},
// 在组件销毁前
// 清除事件监听
beforeDestroy () {
  this.$bus.$off('todoSth');
}
View Code

 资料分享网站

相关推荐