在做增删改查的新增时候,弹窗是表单。发现新增时候并没有对表单重置,并未将其值重置为初始值并移除校验结果

首先看表单是怎么写的:

-----------------------------------错误示例------------------------------------------
<el-form
:model="dataForm"
:rules="dataRule"
ref="dataForm"
v-loading="dataFormLoading"
@keyup.enter.native="dataFormSubmitHandle()"
label-width="80px"
>
<el-row :gutter="20">
<el-col :span="8">
<el-form-item prop="planId" label="名称">
<el-select
v-model="dataForm.planId"
filterable
style="width: 100%"
@change="handlePlant"
>
<el-option
v-for="item in planList"
:key="item.plantid"
:label="item.planname"
:value="item.planid"
></el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
</el-form>

--------------------------------下面为正确示例-----------------------------

<el-form
:model="dataForm"
:rules="dataRule"
ref="dataFormRef"
v-loading="dataFormLoading"
@keyup.enter.native="dataFormSubmitHandle()"
label-width="80px"
>
<el-row :gutter="20">
<el-col :span="8">
<el-form-item prop="planId" label="名称">
<el-select
v-model="dataForm.planId"
filterable
style="width: 100%"
@change="handlePlant"
>
<el-option
v-for="item in planList"
:key="item.plantid"
:label="item.planname"
:value="item.planid"
></el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
</el-form>

然后看resetFields()使用的地方:

init() {
this.visible = true;
this.getPlantList();
this.$nextTick(() => {
// this.$refs["dataForm"].resetFields();//之前写法,ref定义的不要和绑定的数组名字重复
this.$refs["dataFormRef"].resetFields();//绑定ref名字
this.dataForm.plantId = this.storePlantId;
if (this.storePlantId) {
this.getInfo();
}
});
},