全局事件总栈$bus

184 阅读1分钟

$bus是什么

一种组件的通信方式,适用于任意组件之间的联系

解决无关系组件之间的交互联系(兄弟组件之间)

使用

注册

在入口文件main.js原型上添加实例

// event Bus 用于无关系组件间的通信。
Vue.prototype.$bus = new Vue()
传递事件
this.$bus.$emin('事件名',参数)
this.$bus.$emin('send',val)
接收事件
// 在组件的mounted 生命周期里接收
mounted(){
    this.$bus.$on('send',(val) => {
    console.log(val)
    })
}