1、引入mitt
import mitt from "mitt";
// 配置全局事件总线
Vue.prototype.$bus = new mitt();
2、使用
在页面created的时候开启全局监听,befor
this.$bus.emit('refreshList'); //触发
created(){ //被触发时候执行
this.$bus.on("refreshList", () => {
this.handlePage()
this.getWeWorkStaffList()
this.Proportion()
});
},
//关闭
beforeDestroy() {
this.$bus.off('refreshList')
},
总结
// 发送事件
EventBus.$emit('custom-event', payload);
// 接收事件
EventBus.$on('custom-event', (payload) => {
// 处理事件,执行相应操作
});
// 组件销毁时清除事件监听
beforeDestroy() {
EventBus.$off('custom-event');
}