Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

vue 兄弟组件bus通信问题

//common.vue
this.$bus.emit('msg',data)
//a.vue
created(){
    this.$bus.on('msg', params => {})
},
beforeDestroy(){
    this.$bus.$off('msg')
}
//b.vue
created(){
    this.$bus.on('msg', params => {})
},
beforeDestroy(){
    this.$bus.$off('msg')
}

a组件和b组件都调用了公用组件common.vue,a组件销毁前清除了bus,a页面跳转到b页面后,导致接收不到bus的通信了

请问应该怎么解决,不清除会反复接受到数据


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A跳到B
先触发B created,再出发A destroy.

你应该给事件函数加一个名字(可以放到methods里) off的时候仅删除该函数。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...