创建EventBus.js
import vue from 'vue'
export const EventBus = new Vue()
创建两个,没什么关系的组件(目的A组件向B组件发送消息)
在A组件和B组件都引入EventBus
A组件向B组件发送消息
EventBus.$emit('msg','A往B发消息')
B组件接收A组件的消息
EventBus.$on('msg',(arg)=>{
console.log(arg)
})
# B组件进行接收(这里注意使用完EventBus要对EventBus进行销毁,防止对全局污染)
boforeDestroy(){
EventBus.$off('msg')
}