Vue.js - How to call method from another component

Javascriptvue.jsVuejs2Vue Component

Javascript Problem Overview


I am using Vue.Js v2. I want to call component1->c1method in component2->c2method for reload data after submitting.

Vue.component('component1', {
  methods: {
  	c1method: function(){
     alert('this is c1method')
    },
  }
})
Vue.component('component2', {
  methods: {
  	c2method: function(){
     component('component1').c1method()//like this
    },
  }
})

Javascript Solutions


Solution 1 - Javascript

For non-parent-child relation, then this is the same as this one. Call one method, apparently any method of a component from any other component. Just add a $on function to the $root instance and call form any other component accessing the $root and calling $emit function.

On First component

....
mounted() {
this.$root.$on('component1', () => {
// your code goes here
this.c1method()
}
}

and in the second component call the $emit function in $root

...
c2method: function(){
this.$root.$emit('component1') //like this
},

It acts more like a socket. Reference here

https://stackoverflow.com/a/50343039/6090215

Solution 2 - Javascript

// Component A
Vue.component('A', {
	created() {
		this.$root.$refs.A = this;
	},
	methods: {
		foo: function() {
			alert('this is A.foo');
		}
	}
});

// Component B
Vue.component('B', {
	methods: {
		bar: function() {
			this.$root.$refs.A.foo();
		}
	}
});

Solution 3 - Javascript

No need for hacky solutions.
In vuejs we can create events that can be listened globally.
With this feature, whenever we want to call our beloved function, we just emit this event.
Now, we just listen to this event all the time from the component. whenever this global event happens we can execute our method we want to call.

It's pretty simple:

  1. you go to main.js, before creating the vue instance, write this:

export const eventBus = new Vue(); // added line


new Vue({
    ...
    ...
    ...
    render: h => h(App)
}).$mount('#app');


  1. Anywhere we want to fire the target function, we dont fire it, we just emit this event:
eventBus.$emit('fireMethod');
  1. Now in our component that has the target function, we always listen to this event:
created() {
    eventBus.$on('fireMethod', () => {
            this.myBelovedMethod();
    })
}

Dont forget to import eventBus in top.

import {eventBus} from "path/to/main.js";

thats it, few lines of code, no hacky, all vuejs power.

Solution 4 - Javascript

The docs address this situation

https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication

If your components have the same parent, you can emit an event that the parent listens to. Then with the ref property set, you can call the c1method from the parent.

https://vuejs.org/v2/guide/components.html#Child-Component-Refs

Solution 5 - Javascript

Try this out.

<template>
 ...
 <component1 ref='componentOne'>
...
</template>
<script>
  Vue.component('component2', {
    methods: {
     c2method: function(){
       this.$refs.componentOne.c1method();
     }
   }
  });
</script>

Solution 6 - Javascript

If anyone is looking for a solution in Vue.js v3:

https://v3.vuejs.org/guide/migration/events-api.html#event-bus

https://github.com/developit/mitt#install

    import mitt from 'mitt'
    
    const emitter = mitt()
    
    // listen to an event
    emitter.on('foo', e => console.log('foo', e) )
    
    // listen to all events
    emitter.on('*', (type, e) => console.log(type, e) )
    
    // fire an event
    emitter.emit('foo', { a: 'b' })
    
    // clearing all events
    emitter.all.clear()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMiriView Question on Stackoverflow
Solution 1 - JavascriptMir Ayman AliView Answer on Stackoverflow
Solution 2 - JavascriptSajjad ShirazyView Answer on Stackoverflow
Solution 3 - JavascriptPooria HonarmandView Answer on Stackoverflow
Solution 4 - JavascriptEric GuanView Answer on Stackoverflow
Solution 5 - JavascriptAbhishek KanojiaView Answer on Stackoverflow
Solution 6 - JavascriptBhubaView Answer on Stackoverflow