Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

in vue3 script setup style, how to call parent method from a child ?

  1. User

    in vue3 script setup style, how to call parent method from a child ?

  2. Assistant

    You can use the emit function 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 callParentMethod with 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 handleChildMethod to 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.

  3. User

    can I use provide and inject instead ?

  4. 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 parentMethod function using provide. The child component accesses it using inject. The callParentMethod function is called when a button is clicked and it simply calls the parentMethod.

    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.