I am trying to use a method from on of my vue components in another vue component. I have implemented a solution below, however, I think there can be something more reusable.
Here is my problem: I want to call a method from one vue component (which is registered in the instance) in another component inside of it's template in a @click (or alternative) inside of a v-for.
This is my current solution:
Vue Component 1 (this is the component making the call):
Vue.component('person-list', {
props: ['user_id', 'token'],
template: '<div>' +
'<ul>' +
'<li v-for="person in persons" :data-id="person.lk_ID"
class="someItems-list-item">{{ person.name }}</li>' +
'</ul>' +
'</div>',
data: function(){
return {
persons: null,
}
},
methods: {
//some code
},
mounted: function(){
console.log('mounted list');
}
});
Vue Component 2
var someItems= Vue.component('someItems-items-list', {
props: ['g_id', 'token'],
template: '<div>' +
'<ul>' +
'<li v-for="item in someItems">{{ item.name }}</li>' +
'</ul>' +
'</div>',
data: function(){
return {
someItems: null,
}
},
methods: {
callFunction: function (ID = null){
this.$http.post('route', {someID: ID, _token: this.token}).then(function(response){
this.someItems= response.body.items;
});
},
},
mounted: function(){
this.callFunction();
}
});
Instance
const app = new Vue({
el: '#app',
components: {
'someItems-items-list': someItems,
},
});
Solution in Jquery
$(document).on('click','.someItems-list-item',function () {
app.$refs.someItems.callFunction($(this).data('id'));
});
This solution works fine, and its not that much more code, but I think it would be cleaner and easier to maintain if I could call callFunction() like this:
Vue Component 1
template: '<div>' +
'<ul>' +
'<li v-for="person in persons" @click="app.$refs.someItems.callFunction(person.lk_ID)">{{ person.name }}</li>' +
'</ul>' +
'</div>',
However, when I try that, it says that app is undefined. I have also tried the above with this:
Vue Component 1
return {
//other code,
this.someItems: someItems,
}
And then calling in @click="someItems.callFunction(1)" but it also does not work. All these components are inside of app.js. I am working with laravel,vue,jquery.
I feel that I may be missing something in terms of how to connect two vue components to each other, and have been unable to learn it from searching.
@click="app.$refs.someItems..., should be$refs.someItems...