DEV Community

sium_hossain
sium_hossain

Posted on

How to call a method or trigger a function from one component to another in Nuxt

I need trigger a function from one component to another. I don't know it's necessary or not . But whatever here is the solution for you -

Suppose two component. One of them we can called A component and another can be called as B component- So I will trigger A component function from B component.

In component A-

created() {
        this.$root.$refs.A = this;
    },

// and here is the method which I called from `B` component
methods:{
      print(){
        // your others function logic
        console.log('A component function called')

    },
Enter fullscreen mode Exit fullscreen mode

And Now I called this print function from B component via a function.

In component B -

methods:{
  printFromA(){
        this.$root.$refs.A.print();
    }

}
Enter fullscreen mode Exit fullscreen mode

voila!!! check your console 😇
Thank You.

Top comments (0)