【发布时间】:2021-03-02 17:34:49
【问题描述】:
我正在尝试围绕 VeeValidate 4 展开思考,但它与以前的版本有很大不同,因此我遇到了很多问号和障碍。 我使用 Vue 3 应用程序,使用组合 API,在不同的组件/视图中有几个表单。有一个“下一页”按钮可将您带到下一个视图,但由于该按钮与表单完全分开,因此我没有任何 handleSubmit 连接到表单。相反,如果当前表单有效与否,我希望将其保持在状态,这样就可以确定是否允许用户转到下一页。
我想要完成的,但不确定是否可以,是能够在字段验证发生时“挂钩”,并且如果所有表单字段都有效,则以这种方式保存到状态。我已经多次查看文档,但我无法弄清楚。我将不胜感激任何形式的帮助、提示或想法。
这是我的一个 Vue 组件/视图的简化版本:
<template>
<form>
<CustomInput v-model="name" inputType="text" name="name" />
<div v-if="nameErrors.length">{{ nameErrors[0] }}</div>
<CustomInput v-model="width" inputType="text" name="width" />
<div v-if="widthErrors.length">{{ widthErrors[0] }}</div>
</form>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { useField } from 'vee-validate';
import CustomInput from 'CustomInput.vue';
export default defineComponent({
name: 'View',
components: {
CustomInput,
},
setup() {
const { value: name, errors: nameErrors } = useField('name', 'required');
const { value: width, errors: widthErrors } = useField('width', 'required|numeric|boatWidth');
return {
name,
width,
nameErrors,
widthErrors,
};
},
});
</script>
组件层次结构:
LayoutComponent
FormComponent (which component depends on what step the user is on)
NextButtonComponent
【问题讨论】:
标签: typescript vue.js vuejs3 vee-validate vue-composition-api