【问题标题】:When to subscribe to events in Vue lifecycle何时订阅 Vue 生命周期中的事件
【发布时间】:2019-01-15 10:11:08
【问题描述】:

我正在 Vue2 中使用带有子组件的 vue-component 创建一个组件

  <note :bus="bus" :itemId="selectedId"></note>

看起来像这样

  <textarea v-model="text"></textarea>

子组件注册到事件

created() {
  if (this.bus != null) {
    this.bus.$on('store', () => {
      this.store()
    });
  }
}

组件确实

  this.bus.$emit('store')

在所有子组件中触发存储。

现在第一次 store 被调用一次。在第二次编辑时,它被调用了两次,等等。所以我猜每次都会创建更多的子组件。或者我必须从公共汽车上注销?还是我在错误的生命周期挂钩中注册总线?

【问题讨论】:

    标签: typescript vue.js vuejs2 vue-component


    【解决方案1】:

    现在可以使用

    beforeDestroy() {
      if (this.bus != null) {
        this.bus.$off('store', this.store);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 2020-07-26
      • 2019-03-10
      • 1970-01-01
      • 2016-11-06
      • 2014-02-01
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多