in vue3 script setup style, how to call parent method from a child ?
- User
in vue3 script setup style, how to call parent method from a child ?
- Assistant
You can use the
emitfunction to call a method defined on the parent component from the child component.Here is an example where the child component emits an event with the name of the method to be called and its arguments:
<!-- ParentComponent.vue --> <template> <div> <ChildComponent @callParentMethod="handleChildMethod"/> </div> </template> <script> import { defineComponent } from 'vue' import ChildComponent from './ChildComponent.vue' export default defineComponent({ components: { ChildComponent }, methods: { handleChildMethod(methodName, ...args) { if (this[methodName]) { this[methodName](...args) } }, parentMethod(arg1, arg2) { // do something } } }) </script> <!-- ChildComponent.vue --> <template> <div> <button @click="callParentMethod">Call parent method</button> </div> </template> <script> import { defineEmits } from 'vue' export default defineComponent({ emits: ['callParentMethod'], methods: { callParentMethod() { this.$emit('callParentMethod', 'parentMethod', arg1, arg2) // replace `arg1` and `arg2` with the actual arguments } } }) </script>In the above example, the child component emits an event named
callParentMethodwith the name of the parent method to be called as the first argument ('parentMethod') and its arguments (arg1,arg2) as the following arguments.The parent component defines a method
handleChildMethodto handle the emitted event. This method checks if the parent component has a method with the name passed as the first argument and calls it with the remaining arguments. If the parent component has no such method, nothing happens. - User
can I use provide and inject instead ?
- Assistant
Yes, you can use provide and inject to call a parent method from a child component. This is another technique for communicating between components in Vue 3 using props and events.
Here's an example of how to use provide and inject to access and call a parent method from a child component:
<!-- ParentComponent.vue --> <template> <div> <ChildComponent /> </div> </template> <script> import { defineComponent, provide } from 'vue' import ChildComponent from './ChildComponent.vue' export default defineComponent({ components: { ChildComponent }, methods: { parentMethod() { // do something } }, setup() { provide('parentMethod', this.parentMethod) return {} } }) </script> <!-- ChildComponent.vue --> <template> <div> <button @click="callParentMethod">Call parent method</button> </div> </template> <script> import { defineComponent, inject } from 'vue' export default defineComponent({ setup() { const parentMethod = inject('parentMethod') const callParentMethod = () => { parentMethod() } return { callParentMethod } } }) </script>In the code above, the parent component provides the
parentMethodfunction usingprovide. The child component accesses it usinginject. ThecallParentMethodfunction is called when a button is clicked and it simply calls theparentMethod.This approach can be useful when you have deeply nested components and it becomes difficult to pass down props and events through many layers of components. Provide and inject can simplify the communication between components in such cases.