我这里的需求就是当我点击提交时,将数据添加到数组内,并且将表单数据全部恢复初始状态,但是直接修改表单数据的话会触发校验,不能达到我想要的效果,于是使用了官方提供的
resetFields
方法是表单恢复到初始状态,也就是每次点击提交表单校验完毕后重置表单数据,但是这里有有个问题,加上了重置的resetFields
方法后,每次在表单校验完成后一直拿不到this.form
表单的数据,因此这里记录一下解决的方法主要是扩展运算符深拷贝数据
,看如下代码示例
js
<template>
<div class="Curriculum">
<el-button @click="addClass">新增课程</el-button>
<!-- 新增课程弹窗 -->
<el-dialog
title="新增课程"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose"
>
<el-form
:model="form"
:rules="rules"
ref="uploadForm"
label-width="120px"
>
<el-form-item label="名称" prop="className">
<el-input
v-model="form.className"
placeholder="请输入课程名称"
></el-input>
</el-form-item>
<el-form-item label="课程内容" prop="classType">
<el-checkbox-group v-model="form.classType">
<el-checkbox label="跑步机" name="classType1"></el-checkbox>
<el-checkbox label="划船机" name="classType2"></el-checkbox>
<el-checkbox label="地面" name="classType3"></el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item label="课程图标" prop="image">
<input
ref="inputs"
type="file"
name=""
id=""
hidden
@change="handleChange"
/>
<div class="avatar-uploader" @click="uploadImage">
<img v-if="form.image" :src="form.image" class="curriculum-icon" />
<i v-else class="el-icon-plus"></i>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">提交</el-button>
</el-form-item>
</el-form>
</el-dialog>
</div>
</template>
<script>
export default {
name: "Curriculum",
data() {
return {
dialogVisible: false,
form: {
className: "",
classType: [],
image: "", // 用于存储已上传的图片的Base64编码
},
dataList: [], // 课程数据
rules: {
className: [
{ required: true, message: "请输入课程名称", trigger: "blur" },
],
classType: [
{
required: true,
message: "请选择课程内容",
trigger: "change",
},
],
image: [{ required: true, message: "请上传图片", trigger: "change" }],
},
};
},
methods: {
addClass() {
this.dialogVisible = true;
},
handleClose() {
this.$refs.uploadForm.resetFields();
this.dialogVisible = false;
},
uploadImage() {
this.$refs.inputs.click();
},
handleChange() {
var file = this.$refs.inputs.files[0]; // 获取文件对象
if (!file) {
console.log("请选择图片");
return;
}
if (!file.type.startsWith("image")) {
console.log("只能上传图片");
file = null;
return;
}
console.log("file", file);
// 转base64
const reader = new FileReader(); // 创建FileReader对象
reader.onload = (e) => {
this.form.image = e.target.result; // 将读取的结果赋值给form.image
};
reader.readAsDataURL(file); // 读取文件并转换为Base64
},
submitForm() {
this.$refs.uploadForm.validate((valid) => {
if (valid) {
// 模拟添加课程(这里一定要用扩展运算符,不然拿不到this.form数据)
this.dataList.push({ ...this.form });
console.log("this.form", this.form); // 数据里面都是空的拿不到
console.log("this.dataList", this.dataList); // 可以拿到数据
this.$message({
type: "success",
message: "添加成功!",
});
this.$refs.uploadForm.resetFields();
this.dialogVisible = false;
} else {
this.$message({
type: "error",
message: "校验失败!",
});
return false;
}
});
},
},
};
</script>
<style scoped lang="scss">
.avatar-uploader {
width: 100px;
height: 100px;
border: 1px solid #cccccc;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
.curriculum-icon {
width: 100px;
height: 100px;
}
</style>