Vue EventBus&生命周期

发布于 2025-01-21 21:23:19 字数 478 浏览 4 评论 0原文

我有一个问题,即如何在一个组件中使用事件来触发另一个事件。我有两个组成部分,两个孩子都在同一水平上。如果在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2025-01-28 21:23:19

您的问题似乎与不启动更新()有关,您需要更新视图以触发更新()

Your problem seems to be related to not starting the updated(), You need to update the view to trigger the updated()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文