New to Vue.js and trying to handle a submit event in a component representing a form that will check validity of child components and pass on processing to an event handler in the parent if everything looks good.
I'm getting this error... [Vue warn]: Error in v-on handler: "TypeError: undefined is not an object (evaluating '$event.preventDefault')" found in ---> at src/components/MyForm.vue at src/App.vue
Child component is MyForm...
<template lang="pug">
form(@submit.prevent='onFormSubmit', novalidate, autocomplete='on')
slot Content needed in MyForm
</template>
<script lang="ts">
import { defineComponent } from "@vue/composition-api"
export default defineComponent({
name: "MyForm",
setup(_, { emit }) {
const onFormSubmit = () => {
console.log("MyForm:onFormSubmit() - called first")
// Validate child components, if invalid, STOP, otherwise continue...
emit("submit") // errors
}
},
})
Parent component (App)...
<template lang="pug">
#app
.container
MyForm(@submit.prevent='onSubmit')
...other components
</template>
<script lang="ts">
import { defineComponent } from "@vue/composition-api"
import MyForm from "@/components/MyForm.vue"
export default defineComponent({
name: "App",
components: {
MyForm,
},
setup() {
const onSubmit = (): void => {
console.log("App.vue:onSubmit() - called second")
}
return { onSubmit }
}
</script>
Alternatively, would it be better to pass App's onSubmit() function as a prop to MyForm? I could do the validation in MyForm then call the passed-in function?