How to get Access to a Child Method From the Parent in VueJS ? Last Updated : 06 Aug, 2024 Comments Improve Suggest changes Like Article Like Report When working with Vue.js components, it's common to encounter scenarios where you need to access a method inside the parent component that is defined in a child component. This can be useful for triggering specific actions or updating the child component's state.Below are the methods that can be used to access a child component method from a parent component in VueJS.Table of ContentUsing $refsUsing Custom EventsUsing $refsThis approach involves using the $refs property provided by Vue.js to create a reference to the child component. The parent component can then directly call methods on the child component using this reference.Example 1: The below code uses $refs to access the child component method from the parent component in VueJS. HTML <!-- ParentComponent.vue --> <template> <div> <h2>Parent Component</h2> <button @click="callChildMethod"> Call Child Method </button> <child-component ref="childRef"/> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, methods: { callChildMethod() { const childComponent = this.$refs.childRef; if (childComponent) { childComponent.childMethod(); } else { console.error ('Child component reference is undefined.'); } }, }, }; </script> HTML <!-- ChildComponent.vue --> <template> <div> <h3>Child Component</h3> </div> </template> <script> export default { methods: { childMethod() { console.log ('Child method called!'); }, }, }; </script> JavaScript <!-- App.vue --> <template> <div id="app"> <parent-component /> </div> </template> <script> import ParentComponent from './ParentComponent.vue'; export default { components: { ParentComponent, }, }; </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; text-align: center; color: #2c3e50; margin-top: 60px; } </style> Output:Example 2: The below example includes the advanced VueJS code to show some message on the user screen once the button is clicked and child method get accessed. HTML <!-- ParentComponent.vue file --> <template> <div> <h2>Parent Component</h2> <button @click="callChildMethod"> Call Child Method </button> <!-- Display message with transition --> <transition name="fade" mode="out-in"> <div v-if="message" class="message"> {{ message }} </div> </transition> <child-component ref="childRef" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, data() { return { // Message to be displayed message: '', }; }, methods: { callChildMethod() { // Access the child component using $refs const childComponent = this.$refs.childRef; // Check if the child component reference is valid if (childComponent) { // Call the method in the child // component and get the result const result = childComponent.childMethod(); // Update the message based on the result this.message = result; } else { console.error ('Child component reference is undefined.'); } }, }, }; </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter, .fade-leave-to { opacity: 0; } .message { margin-top: 10px; padding: 10px; background-color: #4caf50; color: #ffffff; } </style> JavaScript <!-- ChildComponent.vue file --> <template> <div> <h3>Child Component</h3> </div> </template> <script> export default { methods: { childMethod() { console.log ('Child method called!'); // Your child component logic here // Return a message to the parent return 'Message from Child Component!'; }, }, }; </script> JavaScript <!-- App.vue --> <template> <div id="app"> <parent-component /> </div> </template> <script> import ParentComponent from './components/ParentComponent.vue'; export default { components: { ParentComponent }, }; </script> Output:Using Custom EventsThis approach involves using Vue.js custom events to establish communication between the parent and child components. The child component emits a custom event, and the parent component listens for that event, triggering the desired method.Example: The below code uses custom events to access the child component method from the parent component in VueJS. HTML <!-- ParentComponent.vue --> <template> <div> <h2>Parent Component</h2> <child-component @child-method-triggered="callChildMethod" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, methods: { callChildMethod() { console.log ('Child method called!'); }, }, }; </script> HTML <!-- ChildComponent.vue --> <template> <div> <button @click="triggerChildMethod"> Call Child Method </button> <h3>Child Component</h3> </div> </template> <script> export default { methods: { triggerChildMethod() { this.$emit ('child-method-triggered'); }, }, }; </script> HTML <!-- App.vue --> <template> <div id="app"> <parent-component/> </div> </template> <script> import ParentComponent from './components/ParentComponent.vue'; export default { components: { ParentComponent, }, }; </script> <script> import { createApp } from 'vue'; import App from './App.vue'; createApp(App).mount('#app'); </script> Output: Comment More infoAdvertise with us Next Article How to get Access to a Child Method From the Parent in VueJS ? S sahiltrin8xk Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League JP Morgan TCS-coding-questions Volkswagen IT Services Vue.JS Geeks Premier League 2023 +4 More Similar Reads How to Access Child Method From the Parent in VueJS ? In this article, we are going to discuss how can we access the child method from the parent in VueJS. We need to create such a component that shows the use of accessing the child methods from the parent component. Approach:We will create two components ParentMethod and ChildComponent.ParentMethod vu 3 min read How to Call a Parent Method from a Child Component in Vue 3 ? Vue.js is a JavaScript framework for building user interfaces. In Vue 3 parent and child component can communicate with each other through props and events. Props are used to pass the data from the parent to the child component and events are used to pass the data from child to parent component. The 3 min read How to Get Query Parameters from a URL in VueJS ? Query parameters are part of a URL that assigns values to specified parameters. They start after the question mark and are separated by ampersands ("&") in the URL. For example, in the URL https://example.com?name=John&age=23, name=John and age=23 are query parameters. The below-listed metho 8 min read How to call a parent method from child class in JavaScript? Inheritance in programming allows a class to derive methods and properties from another class, similar to how a child inherits traits from its parents. A child class can have its properties in addition to inherited ones. The deriving class is called a derived, sub, or child class, while the class it 3 min read How to Update Parent Data from Child Component in VueJS? In Vue.js, communication between components is essential for building dynamic applications. One common scenario is updating the parent component's data from a child component. we will cover several ways to achieve this, including custom events, and v-model. Updating parent data from a child componen 3 min read How to Pass Value from One Child Component to Another in VueJS ? Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. In this article, we will learn how to pass value from one child component to a 3 min read How to listen for Prop Changes in VueJS ? In Vue.js, props provide the mechanism for parent components to share data with their child components. While props themselves are reactive, meaning changes in the parent component automatically trigger a re-render of the child component, sometimes we might need to react to specific prop changes wit 3 min read How to loop through a list of elements and display it in VueJS ? Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin 2 min read How to communicate from parent component to the child component in Angular 9 ? Angular makes the communication between components very easy. In this article, we will learn how to communicate from a parent component to the child component. Approach: Let's create two components: parent child In the parent component, declare the property that you want to receive in the child comp 2 min read Parent to Child Communication with Metadata in Angular 17 A parent component in Angular can transfer data or information to its child components using parent-to-child communication. This is an essential component of creating reusable and modular Angular applications. This is an overview of Angular 17's parent-to-child communication features. PrerequisitesB 2 min read Like