我需要处理子组件中的事件;检查某个条件;如果为真,则将“提交”事件发送回父级,以便它的事件处理程序将运行。
下面的示例使用 Vue.js 2.6.11 触发父级的事件处理程序(将“vue”替换为“@vue/composition-api”)。使用 3.0.0-rc.5,它会触发两次。想知道这是故意的更改,错误还是我。
应用程序.vue:
<template lang="pug">
#app
.container
h1 Form Experiment
FormGroup(@submit='onSubmit')
button.btn.btn-primary(type='submit') Submit
</template>
<script lang="ts">
import { defineComponent } from "vue"
import FormGroup from "@/components/FormGroup.vue"
export default defineComponent({
name: "App",
components: {
FormGroup,
},
setup() {
const onSubmit = (): void => {
alert("Parent event handler")
}
return { onSubmit }
}
})
</script>
FormGroup.vue:
<template lang="pug">
form(@submit.prevent='onFormSubmit', novalidate, autocomplete='on')
slot Content needed in FormGroup
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "FormGroup",
setup(_, { emit }) {
const check = ref(true) // evaluated elsewhere - simplified for this example
const onFormSubmit = (): void => {
if (check.value) {
alert("Form is valid - sending event to parent")
emit("submit")
} else {
alert("Form is not valid.") // so don't emit the event to parent
}
}
return { check, onFormSubmit }
}
})
</script>
关于为什么onSubmit()在 Vue.js 3.0.0-rc.5 中触发两次的任何想法?