效果图
代码
<template>
<u-form :model="form" ref="formRole" :rules="rules">
<u-form-item prop="nickname">
<u-input v-model="form.nickname" placeholder="姓名" border="none" />
</u-form-item>
<u-form-item prop="password">
<u-input v-model="form.password" placeholder="密码" border="none" />
</u-form-item>
<button @click="submit" class="btn">提交</button>
</u-form>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
const formRole = ref<any>(null);
const rules = {
'password': {
type: 'string',
required: true,
message: '请输入密码',
trigger: ['blur', 'change'],
},
'nickname': {
type: 'string',
required: true,
message: '请输入名称',
trigger: ['blur', 'change'],
}
}
const form = reactive({
nickname: null,
password: null,
});
const submit = () => {
console.log(formRole.value, 'formRole.value');
formRole.value.validate().then(res => {
// uni.$u.toast('校验通过')
console.log(res, '成功');
// return
}).catch(err => {
console.log(err, '校验失败');
// return
})
};
</script>
经验之谈
1. :model=‘form’
若把form的值设置为 空对象 const form = reactive({ });
控制台报错
文章来源:https://www.toymoban.com/news/detail-725494.html
2. 不能把 prop 写为name 否则没有任何效果
文章来源地址https://www.toymoban.com/news/detail-725494.html
到了这里,关于微信小程序/vue3/uview-plus form兜底校验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!