1.全局事件总线$bus配置:
// main.js中
const vm = new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this //安装全局事件总线 其中这个this指的是Vue实例vm
},
})
// moduleA.vue // 发送数据
methods: {
sendData () {
// 通过修改Vue原型链的方式注册
this.$event.$emit('sendSata', "冬雨");
// 直接注册在window上
}
moduleB.vue //接受数据
mounted(){
this.$event.$on('sendSata', data=>{
this.name = data;
}
注:兄弟组件通信可使用这种方式,方便快捷。