一、配置
在main.js中配置:
new Vue({
render: h => h(App),
// 全局事件总线$bus的配置
beforeCreate(){
Vue.prototype.$bus = this;
}
}).$mount('#app')
二、使用
例如:在Search组件中通知兄弟组件Header清除关键字。
实现:
在Search组件中:
// 通知兄弟组件Header清除关键字
this.$bus.$emit("clear");
在Header组件中:
mounted() {
// 通过全局事件总线清除关键字
this.$bus.$on("clear",()=>{
this.keyword = '';
})
}