vue 里bus的使用

兄弟组件之间进行传值(非父子组件);


安装:npm install vue-bus


 

在main.js中引入vue-bus

 


 

 

import Vue from 'vue';
import VueBus from 'vue-bus';
 
Vue.use(VueBus);

 


在组件中使用:


 

A组件触发事件:

this.$bus.emit("parameterName",params);

 


 

B组件接收事件:

this.$bus.on("parameterName",function(value){

  console.log(value)
});
 

组件销毁时解除事件绑定:

destroyed:function(){

  this.$bus.off("parameterName")

}

 
 
posted @ 2020-08-21 16:08  喆星高照  阅读(246)  评论(0编辑  收藏  举报