1. 在main.js
生成vue实例时绑定$bus
new Vue({
el: '#app',
components: { App },
template: '<App/>',
beforeCreate() {
//绑定全局事件总线
Vue.prototype.$bus = this;
}
})
2. 在A组件中触发事件名和传参
this.$bus.$emit('refresh', '111') //两个参数,一个是方法名,一个是要传的参数
3. 在B组件中监听该方法
created() {
this.$bus.$on('refresh',(data)=>{
console.log(data);
})
}