'Vue2 call a method of other component [closed]

I have a method: openMenu() in component A.

I want to call this method in component B.

There are $root, Event bus, and Vuex, what should I do in this case?

What is the best practice?



Solution 1:[1]

you can use the emit.

in the component A:

mounted() {
 this.$root.$on("callMyMethod", () => {
   this.myMethod();
 });
},
methods: {
  myMethod() {
    // do something
  },
},

and in the component B:

methods: {
 doSomething() {
    this.$root.$emit("callMyMethod");
  },
},

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raya Sabari