思路:使用Promise.all以及elementui自带的表单校验去判断
html
<el-form ref="form" :model="formData">
....
</el-form>
<el-form ref="formTwo" :model="formDataTwo">
....
</el-form>
<el-form ref="formThird" :model="formDataThird">
....
</el-form>
...
<span slot="footer" class="dialog-footer">
<el-button @click="handleClickCancel">{{ '取消' }}</el-button>
<el-button type="primary" @click="validFormHandle">{{ '确定' }}</el-button>
</span>
需求:所有表单通过校验后才可以请求接口
javascript
/**
* @description: 确定的点击事件
* @return {*}
*/
validFormHandle() {
Promise.all(['formData', 'formDataTwo','formDataThird'].map(item => this.validForm(item))).then(() => {
this.handleClickOk()
}).catch(error => {
this.$message({
showClose: true,
message: '请先检查表单是否都已填写',
type: 'warning'
})
console.log('error', error)
})
},
handleClickOk() {
const params = { ...this.formData, ...this.formDataTwo,...this.formDataThird }
//请求接口处理
xxx(params).then(res =>{
this.$message.success(res.msg)
)
},
// 校验表单
validForm(formRef) {
return new Promise((resovle, reject) => {
this.$refs[formRef].validate(valid => {
if (valid) {
resovle()
} else {
reject()
}
})
})
},