出现问题的代码
javascript
<!-- 密码重置弹框 -->
<el-dialog v-model="innerVisible" width="500" title="密码重置" append-to-body>
<el-form ref="ruleFormRef" style="max-width: 600px" :model="passForm" status-icon :rules="rules"
label-width="auto" class="demo-ruleForm">
<el-form-item label="密码" prop="password">
<el-input v-model="passForm.password" type="password" autocomplete="off" />
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input v-model="passForm.checkPass" type="password" autocomplete="off" />
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button @click="innerVisible = false">取消</el-button>
<el-button type="primary" @click="resetPass">
提交
</el-button>
</div>
</template>
</el-dialog>
javascript
// 密码重置
const resetPass = (formEl: FormInstance | undefined) => {
if (!formEl) return
formEl.validate((valid) => {
if (valid) {
passForm.id = user.value.id
saveOrUpdate(passForm)
.then((res: any) => {
if (res.code === 200) {
innerVisible.value = false;
ElMessage.success('提交成功!');
// 刷新一下用户信息
getUserData()
}
}).catch((error) => {
ElMessage.error(error.message)
});
} else {
console.log('error submit!')
}
})
}
发现点击提交时,进行校验逻辑时报错,原因是提交表单的按钮那里没有给方法传ruleFormRef:
正确写法
javascript
<el-button type="primary" @click="resetPass(ruleFormRef)">提交</el-button>