[Vuejs]-Created from Vuejs is called on every second

0πŸ‘

In your first component, why don’t you just use mounted or destroyed hooks to check for required route changes.

mounted() {
 if (this.$route.path === "NewGame") {
   EventBus.$emit("changeRoute", true);
 }
} 

0πŸ‘

I found out a solution that works for me. I have to user $once insted of $on

EventBus.$once("changeRoute", payload => {
  this.myMethod();  //now is called just once
});

0πŸ‘

Add the following after your created():

destroyed() {
  EventBus.$off("changeRoute", payload => {
    this.myMethod();
  });
},

Notice that instead of $on I wrote $off, this will remove the event listener.
Make sure that when you attach a listener, to always remove it when the component is destroyed, otherwise events will be registered several times.

Not entirely sure this will fix your problem as we do not have the full context on how you are using the components, but hopefully it fixes your problem.

Leave a comment