前言:最近项目中遇到多个表单需要一起校验的情况,记录一下!
一、解决方案
使用promise接收多个表单校验返回信息
// 表单校验
checkForm(formName) {
const result = new Promise((res, rej) => {
this.$refs[formName].validate((valid) => {
if (valid) {
res()
}
})
})
this.checkList.push(result) // data 中定义一个接收结果集的数组
}
// 保存
submit() {
this.checkForm('userForm') // 校验对应的form表单
this.checkForm('projectForm')
Promise.all(this.checkList).then(() => {
console.log('校验完成!!');
})
}
promise.all().then() 等待所有都完成(或第一个失败)!