Absolutely!
Calling a parent method from a child component in Vue is totally achievable and quite straightforward.
Every Vue component has access to the $parent
property, which allows you to invoke any method present in the parent component.
If you'd like to see an example of this in action, check out this JSFiddle: https://jsfiddle.net/50qt9ce3/1/
<script src="https://unpkg.com/vue"></script>
<template id="child-template">
<span @click="someMethod">Click me!</span>
</template>
<div id="app">
<child></child>
</div>
<script>
Vue.component('child', {
template: '#child-template',
methods: {
someMethod(){
this.$parent.someMethod();
}
}
});
var app = new Vue({
el: '#app',
methods: {
someMethod(){
alert('parent');
}
}
});
</script>
Please Note: While it's generally not advised to rely heavily on calling parent methods from child components when designing disconnected and reusable components, there are scenarios where building interdependent non-reusable components can benefit greatly from this approach.