Home > Mobile >  Vue parent component get response from child method
Vue parent component get response from child method

Time:06-22

I have a parent component and a child. From parent i want to call a method in child component and wait the response. How can i do that? I need the response for adding a condition in the parent method.

<!-- MY CHILD COMPONENT -->
<my-child-component ref="childform" />

// parent method
methods: {
     callChildMethod() {
        this.$refs.childform.onSubmit()
        // if want to get the response and do something
        if (response) {
            //do something
        }
      },
}

//child method
methods: {
    onSubmit() {
      //do something
      return false
    },
}

CodePudding user response:

Your problem could be solved using refs and it works as follows: In parent component:

 <parent @click="clickHandler">
    <child ref="child" />
 </parent>

In your parent script tag:

methods: {
   clickHandler(){
      //call child component's method using reference give in parent component
         await this.$refs.child.child_method_name();
      // rest of your parent code herer
   }
}
  • Related