1.它是一种组件间通信的方式,适用于任意组件间通信
;
2.安装全局事件总线:
new Vue({
.........
beforeCreate(){
Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm
}
.........
})
3.使用事件总线:
(a).接受数据:A
组件想接受数据,则在A
组件中给$bus
绑定自定义事件,事件的回调留在A
组件自身。
methods(){
demo(data){.........}
}
......
mounted(){
this.$bus.$on('xxx',this.demo)
}
(b).提供数据:this.$bus.$emit('xxx',数据)
4.最好在beforeDestroy
钩子中,用$off
去解绑当前组件所用到的事件;