1.form表单 上传自己看着写
<el-form ref="formOut" :model="formData" :rules="formRules" label-width="120px">
<el-form-item label="图标" prop="img">
<div style="display: flex">
<el-upload class="upload-demo" :action="url + '/file/put-file'" :on-success="(res) => {onSuccess(res, 'formData');}" :limit="1" :file-list="fileList" :show-file-list="false" ref="uploadImg1">
<div v-if="formData.img">
<img :src="formData.img" alt="" style="width: 50px; height: 50px" />
<i class="el-icon-delete iconClass_del" @click.stop="delPic('formData')"></i>
</div>
<el-button v-else size="small" type="primary">点击上传</el-button>
</el-upload>
</div>
</el-form-item>
</el-form>
2.rules验证规则
formRules: {
img: [
{
required: true,
validator: this.fileMustUpload,
trigger: "change",
},
],
}
3.自定义验证方法
fileMustUpload(rule, value, callback) {
if (this.formData.img == null) {
// 未上传文件
callback("请上传图标");
}
callback();//不要忘记写这个callback
},