I also have the same problem statement, where I have my validations method in parent and wants to check that in child component, before performing the save action.
If you want to perform something like this. then a better option is to pass the method as props. It may not make sense to await for the response of $emit
.
Example:
The method that I need to call is doCustomValidation().
of parent-component from child-component's save()
method.
Parent Codebase (shown only the required codebase where names matching to the asked question):
<template>
<child-component
:doCustomValidation="doCustomValidation"
/>
</template>
<script lang="ts">
import ChildComponent from "@/students/ChildComponent.vue";
export default defineComponent({
components: {
"child-component": ChildComponent,
},
methods: {
doCustomValidation() {
let isValid = false;
// do your stuff here, even you can access the
// validation params and show the validations here.
return isValid;
}
}
})
</script>
Child Codebase (shown only the required codebase where names matching to the asked question):
<script lang="ts">
export default defineComponent({
props: ["doCustomValidation"],
methods: {
save() {
// calling parent validation method and
// this will wait for result.
if(this.doCustomValidation()) {
// perform Save here.
}
}
}
)}
</script>