前言:在学会父子之间的通信后,下面开始学习兄弟组件之间的通信。
1、介绍:
使用:兄弟组件之间进行传值;
2、安装:
npm install vue-bus
3、在main.js中引入vue-bus:
import Vue from 'vue';
import VueBus from 'vue-bus';
Vue.use(VueBus);
4、在组件中使用:
A组件触发事件:
this.$bus.emit("parameterName",params);
B组件接收事件:
this.$bus.on("parameterName",function(value){
console.log(value)
});
组件销毁时解除事件绑定:
destroyed:function(){
this.$bus.off("parameterName")
}