Vue EventBus&生命周期
我有一个问题,即如何在一个组件中使用事件来触发另一个事件。我有两个组成部分,两个孩子都在同一水平上。如果在component1.vue中调用了更新()方法,则我希望component2.vue方法setValues()启动。
这就是我在component.2.vue上创建的内容:
created() {
EventBus.$on('updatedComponent', () => {
this.setValues();
});
}
这是在component1.vue中的更新()方法中:
EventBus.$emit('updatedComponent');
我已经在两个组件中都导入了EventBus,并且Event Bus确实可以使用(我在$中使用了Console log进行了测试。开启),但它仍然不起作用。我需要放置我的EventBus。
I have a question about how to use an event in one component to trigger an event in another. I have two components, both child components at the same level. If the updated() method is called in component1.vue, I want component2.vue method setValues() to fire.
This is what I have within created on component2.vue:
created() {
EventBus.$on('updatedComponent', () => {
this.setValues();
});
}
And this is within the updated() method in component1.vue:
EventBus.$emit('updatedComponent');
I have eventbus imported already in both components, and event bus does work (I tested with a console log within the $on) but it still does not work. Do I need to be placing my EventBus.$on in a different lifecycle hook?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题似乎与不启动更新()有关,您需要更新视图以触发更新()
Your problem seems to be related to not starting the updated(), You need to update the view to trigger the updated()