组件之间使用this.$bus.$on传值之前需要先this.$bus.$off注销事件

版权声明:17602128911 https://blog.csdn.net/bus_lupe/article/details/85341889

this.$bus是全局变量

a、b是两个父组件,c是子组件。

c页面触发事件:

this.$bus.$emit(event)

a、b页面监听c组件的事件

pagea:

this.$bus.$on(event, () => {
    this.status = 'reserve'
})

pageb:

this.$bus.$on(event, () => {
     this.status = 'buying'
})

如果在调用了a页面之后,再调用b页面,回导致this.status是reserve而不是我们想要的buying。
正确写法:
pagea:

this.$bus.$off(event).$on(event, () => {
    this.status = 'reserve'
})

pageb:

this.$bus.$off(event).$on(event, () => {
     this.status = 'buying'
})

猜你喜欢

转载自blog.csdn.net/bus_lupe/article/details/85341889