npm install vue-bus
import Vue from ‘vue‘
import VueBus from ‘vue-bus‘
Vue.use(VueBus)
在想传递出去的组件中触发事件:this.$bus.emit
<script>
export default{
methods:{
testBus(){
this.$bus.emit(‘foo‘,{message:‘来自vue-bus的消息‘});
}
}
}
</script>
?
在想要接收数据的页面监听事件:this.$bus.on
<script>
export default{
created(){
this.$bus.on("foo",this.getMessage);
},
beforeDestory(){
this.$bus.off("foo",this.getMessage);
},
methods:{
getMessage(params){
console.log(params.message);
}
}
}
</script>
注:
原文:https://www.cnblogs.com/scorpiozone/p/14799352.html