我正在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')
触发存储在所有子组件中。
现在第一次存储被调用一次。在第二次编辑中,它被称为两次,依此类推。所以我猜每次都创建更多的子组件。还是我必须从公共汽车上注销?还是我在错误的生命周期挂钩中向总线注册?
答案 0 :(得分:1)
现在可以使用
beforeDestroy() {
if (this.bus != null) {
this.bus.$off('store', this.store);
}
}