Miri Miri -4 years ago 561
Javascript Question

Vue.js - How to call method from another component

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
},
}
})

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download