vue3實(shí)現(xiàn)父組件提交校驗(yàn)多個(gè)子組件
實(shí)現(xiàn)功能:在父組件提交事件中校驗(yàn)多個(gè)子組件中的form
父組件:
<script setup lang="ts"> import {ref, reactive} from 'vue' import childForm from './childForm.vue' import childForm2 from './childForm2.vue' let approvalRef = ref() let approvalRef2 = ref() let resultArr= reactive([])//存放子組件的數(shù)組 let errListMsg =ref("")//用來存儲(chǔ)錯(cuò)誤提示 //這個(gè)方法是固定的,用來創(chuàng)建 Promise 實(shí)例,為多個(gè)組件校驗(yàn)使用 const checkForm = (formChild) =>{ let result = new Promise((resolve,reject)=>{ formChild.validate((valid,fields)=>{ if (valid) { console.log('submit'); resolve() }else{ console.log('error'); Object.keys(fields).forEach((v,index)=>{ if (index==0) { const PropName = fields[v][0].field formChild.scrollToField(PropName) errListMsg.value = fields[v][0].message } }) reject() } }) }) resultArr.push(result) } //提交按鈕事件 const taskFun = ()=>{ //獲取該子組件暴露出來的form 的 ref const approvalRefChild = approvalRef.value.forms const approvalRefChild2 = approvalRef2.value.ruleFormRef //調(diào)用上面創(chuàng)建好的方法 checkForm(approvalRefChild) checkForm(approvalRefChild2) Promise.all(resultArr).then((results)=>{ console.log('這里是接口請(qǐng)求'); //校驗(yàn)通過 }).catch(err=>{ //校驗(yàn)不通過提示 console.log(errListMsg.value); }) resultArr=[]//每次請(qǐng)求完要清空數(shù)組 errListMsg.value=""http://提示也需要清空 } </script> <template> <childForm ref="approvalRef"></childForm> <childForm2 ref="approvalRef2"></childForm2> <button @click="taskFun">提交</button> </template>
子組件一:
這個(gè)是表格可以增刪改的情況,對(duì)表格添加輸入校驗(yàn)
<script setup lang="ts"> import {ref, reactive} from 'vue' import type { FormInstance } from 'element-plus' const forms = ref<FormInstance>() let info:any = reactive({ data:[{name:'1'}] }) const formRules = reactive({ name: [{ required: true, message: '請(qǐng)輸入姓名', trigger: 'change' }], role: [{ required: true, message: '請(qǐng)選擇', trigger: 'blur' }] }) defineExpose({ forms }) </script> <template> <el-form :model="info" ref="forms"> <el-table ref="tableRef" :data="info.data" border> <el-table-column align="center" property="name" label="*姓名"> <template #default="{row,$index}"> <el-form-item :prop="`data[${$index}].name`" :rules="formRules.name"> <el-input placeholder="請(qǐng)輸入姓名" v-model="info.data[$index].name" /> </el-form-item> </template> </el-table-column> <el-table-column align="center" property="role" label="角色"> <template #default="{row,$index}"> <el-form-item :prop="`data[${$index}].role`" :rules="formRules.role"> <el-input placeholder="請(qǐng)輸角色" v-model="info.data[$index].role" /> </el-form-item> </template> </el-table-column> </el-table> </el-form> </template>
子組件二:
這個(gè)是普通的form表單情況
<template> 第二個(gè)組件 <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" :size="formSize" status-icon > <el-form-item label="Activity name" prop="name"> <el-input v-model="ruleForm.name" /> </el-form-item> </el-form> </template> <script lang="ts" setup> import { reactive, ref } from 'vue' import type { FormInstance, FormRules } from 'element-plus' interface RuleForm { name: string } const formSize = ref('default') const ruleFormRef = ref<FormInstance>() const ruleForm = reactive<RuleForm>({ name: 'Hello' }) const rules = reactive<FormRules<RuleForm>>({ name: [ { required: true, message: '請(qǐng)輸入Activity name', trigger: 'blur' }, { min: 3, max: 5, message: '長度再3-5位', trigger: 'blur' }, ] }) defineExpose({ruleFormRef}) </script>
注意:
1、子組件中的form要添加ref屬性,并使用defineExpose暴露出去,這樣父組件才能獲取到子組件中的ref,才能進(jìn)行校驗(yàn)
2、當(dāng)對(duì)表格輸入內(nèi)容做校驗(yàn)時(shí)prop和v-model綁定的是同一個(gè)才能校驗(yàn)通過
3、當(dāng)發(fā)現(xiàn)校驗(yàn)一直不通過時(shí)可以查看控制臺(tái)打印的的結(jié)果,看哪個(gè)校驗(yàn)沒通過
校驗(yàn)不通過
到此這篇關(guān)于vue3實(shí)現(xiàn)父組件提交校驗(yàn)多個(gè)子組件的文章就介紹到這了,更多相關(guān)vue3父組件校驗(yàn)多個(gè)子組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-extend和vue-component注冊(cè)一個(gè)全局組件方式
這篇文章主要介紹了vue-extend和vue-component注冊(cè)一個(gè)全局組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Vue中使用Lodop插件實(shí)現(xiàn)打印功能的簡單方法
這篇文章主要給大家介紹了關(guān)于Vue中使用Lodop插件實(shí)現(xiàn)打印功能的簡單方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Vuejs入門教程之Vue生命周期,數(shù)據(jù),手動(dòng)掛載,指令,過濾器
本篇文章主要介紹了Vuejs入門教程之Vue生命周期,數(shù)據(jù),手動(dòng)掛載,指令,過濾器的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Vue+OpenLayers?創(chuàng)建地圖并顯示鼠標(biāo)所在經(jīng)緯度(完整代碼)
這篇文章主要介紹了Vue+OpenLayers?創(chuàng)建地圖并顯示鼠標(biāo)所在經(jīng)緯度,本文使用的是高德地圖,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11一文教會(huì)你如何運(yùn)行vue項(xiàng)目
最近因?yàn)楣卷?xiàng)目問題,開始學(xué)習(xí)vue,這篇文章主要給大家介紹了關(guān)于如何運(yùn)行vue項(xiàng)目的相關(guān)資料,文中還介紹了如何運(yùn)行別人的項(xiàng)目,需要的朋友可以參考下2022-06-06第一次在Vue中完整使用AJAX請(qǐng)求和axios.js的實(shí)戰(zhàn)記錄
AJAX是現(xiàn)代Web開發(fā)的一個(gè)關(guān)鍵部分,盡管它一開始看起來令人生畏,但在你的武庫中擁有它是必須的,下面這篇文章主要給大家介紹了關(guān)于第一次在Vue中完整使用AJAX請(qǐng)求和axios.js的相關(guān)資料,需要的朋友可以參考下2022-11-11vue3?實(shí)現(xiàn)關(guān)于?el-table?表格組件的封裝及調(diào)用方法
這篇文章主要介紹了vue3?實(shí)現(xiàn)關(guān)于?el-table?表格組件的封裝及調(diào)用方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06vue封裝echarts組件,數(shù)據(jù)動(dòng)態(tài)渲染方式
這篇文章主要介紹了vue封裝echarts組件,數(shù)據(jù)動(dòng)態(tài)渲染方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Vue兩個(gè)同級(jí)組件傳值實(shí)現(xiàn)
Vue組件之間是有聯(lián)系的,避免不了組件之間要互相傳值,那么如何實(shí)現(xiàn)Vue兩個(gè)同級(jí)組件傳值,本文就來介紹一下,感興趣的可以了解一下2021-07-07