-1

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?

hannahatl
  • 91
  • 1
  • 8
  • The question lacks https://stackoverflow.com/help/mcve . It's unknown what's EventBus. Is it a separate Vue instance? Then the answer is that it's unrelated to the lifecycle of components where it's used. "event bus does work (I tested with a console log within the $on) but it still does not work" - what is the meaning of this? – Estus Flask Apr 14 '22 at 22:08
  • `And this is within the updated() method` - that can mean two different things, Is it updated lifecycle hook or updated method inside methods section? Best thing is instead of describing such things using words, show the surrounding code just enough to understand where and what it is doing. `I tested with a console log within the $on` - Does that mean `setValues` is getting called but not doing what it is suppoosed to do? Then you should show what is in inside `setValues` – Mat J Apr 15 '22 at 08:15

1 Answers1

0

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

BaLaBaLa
  • 63
  • 5