vue中$EventBus被多次触发

118 阅读1分钟

问题描述 在a页面想触发b页面的方法 使用 $EventBus的时候多次触发

触发b页面的topays

this.$EventBus.$emit("topays", parmars)

接收a页面的参数

this.$EventBus.$on("topays", (res) => {
    this.Buying = res.continueBuying;
    this.goodIdSn = res.sn;
    console.log("11111111111111");
    this.payMoeny();
})

代码在这样的情况下 输出log里面的东西会被多次触发

解决办法: 在每次绑定事件前,先解绑该事件:

 this.$EventBus.$off('topays');   
 this.$EventBus.$on("topays", (res) => {
    this.Buying = res.continueBuying;
    this.goodIdSn = res.sn;
    console.log("11111111111111");
    this.payMoeny();
})

之后log输出就只有一次了。