什么是事件总线?有一个全局的EventBus所有事件都订阅它所有组件也发布给它,订阅组件得到更新所有组件都能够发布事件到总线,然后总线被另一个组件订阅,然后订阅它的组件将获取更新详情请点这里,看这位大佬用Es6的class类模拟事件总线创建一个负责事件派发的Bus类。监听和回调管理类Bus{constructor(){//eventName1:[fn1,fn2],//eventName2:[fn3,fn4]//}this.callbacks={}}$on(name,fn){this.callbacks[name]=this.callbacks[name]this.callbacks[name].push(fn)}$emit(name,args){if(this.callbacks[name]){this.callbacks[name].forEach(cb=>cb(args))}}}//main.jsVue.prototype.$bus=newBus()//child1this.$bus.$on('foo',handle)//child2this.$总线。$emit('foo')
