最近在工作中,使用了消息总线(message bus)这么一个东西,想起 Vue 也有类似的东西,所以想看一下其源码实现。
学习英语是理解编程的第一步。bus 有公共汽车的意思,也有 ‘a set of wires that carries information from one part of a computer system to another’(计算机系统的总线)。所以这里 bus 的意思是总线,一种形象的说法,为了更进一步的了解这个概念,可能需要一点硬件知识。
完整代码
总结
bus 的实现是大同小异的,好像也没什么特别要说的,满足一下轻微的好奇心。想了解实现的细节,可以查看例子的详细代码。
参考链接
- Using event bus in Vue.js to pass data between components
- vue 源码中的实现:https://github.com/vuejs/vue/blob/dev/src/core/instance/events.js
《VueJS 中 event bus 的实现》有一个想法