admin管理员组文章数量:1287591
I'm building the logic for an 'undo delete' - action. For that, I'm using an Event Bus to send an event to two unrelated ponents like so:
Undo.vue:
EventBus.$emit(`confirm-delete-${this.category}`, this.item.id);
The name of the event(this.category
) is based on props ing from a parent (ConfirmDeleteModal.vue
) and then received as follows:
CategoryA.vue
created() {
EventBus.$on('confirm-delete-category-a', (id) => {
this.confirmDelete(id);
});
},
CategoryB.vue
created() {
EventBus.$on('confirm-delete-category-b', (id) => {
this.confirmDelete(id);
});
},
My issue: For some reason, the event for category-a
is emitted and received twice (category-b
works fine). I have to listen to the confirm-event
constantly, therefore I can't remove the event listener after I received the event or listen only $once
. Any ideas how to solve this (maybe with Vuex)? Thanks!
I'm building the logic for an 'undo delete' - action. For that, I'm using an Event Bus to send an event to two unrelated ponents like so:
Undo.vue:
EventBus.$emit(`confirm-delete-${this.category}`, this.item.id);
The name of the event(this.category
) is based on props ing from a parent (ConfirmDeleteModal.vue
) and then received as follows:
CategoryA.vue
created() {
EventBus.$on('confirm-delete-category-a', (id) => {
this.confirmDelete(id);
});
},
CategoryB.vue
created() {
EventBus.$on('confirm-delete-category-b', (id) => {
this.confirmDelete(id);
});
},
My issue: For some reason, the event for category-a
is emitted and received twice (category-b
works fine). I have to listen to the confirm-event
constantly, therefore I can't remove the event listener after I received the event or listen only $once
. Any ideas how to solve this (maybe with Vuex)? Thanks!
-
1
Probably because
created
runs twice? Did you debug it? Depends on how you use these ponents. – Estus Flask Commented Feb 25, 2020 at 9:11 -
But CategoryA and CategoryB are different ponents, so
created
doesn't really run twice, does it? – suuuriam Commented Feb 25, 2020 at 9:20 - Since category-a is received twice and category-b is not, there's clearly a difference between A and B somewhere. Please, provide stackoverflow./help/mcve that can replicate the problem. – Estus Flask Commented Feb 25, 2020 at 9:23
1 Answer
Reset to default 14I was also facing the same kind of issue and then after some research, I got this solution.
You have defined your ponent CategoryA.vue
two times in your application which is causing the function to run twice which is in this case CategoryA.vue
Solution:
You have to call destroyed()
after the created()
Like:
created() {
EventBus.$on('confirm-delete-category-a', (id) => {
this.confirmDelete(id);
}),
},
destroyed() {
EventBus.$off('confirm-delete-category-a');
},
本文标签: javascriptVuejs EventBus being called multiple timesStack Overflow
版权声明:本文标题:javascript - Vue.js: EventBus being called multiple times - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277189a2369798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论