vue elementUI 上傳非空驗(yàn)證示例代碼

<el-form-item label="照片" prop="staffImg">
<template v-slot:label>
<span v-show="!rules.staffImg[0].required"
style="color: #ff4949;margin-right: 4px;">*</span>
<span>照片</span>
</template>
<el-upload class="avatar-uploader" :action="action" ref="upload" :show-file-list="false"
:on-change="uploadChange" :headers="headers" :before-upload="fileBeforeUpload"
:on-success="fileUploadSuccess" accept=".jpg,.png">
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item> // 表單校驗(yàn)
rules: {
staffImg: [
{ required: true, message: "圖片不能為空", trigger: ["blur", 'change'] },
],
}, computed: {
// 上傳頭像
action() {
let baseUrl = "";
if (process.env.NODE_ENV === "development") {
baseUrl = process.env.VUE_APP_BASE_API + "/base/common/uploadFile";
} else {
baseUrl = process.env.VUE_APP_BASE_API + "/base/common/uploadFile";
}
return baseUrl;
},
}, uploadChange(file, fileList) {
// console.log("上傳進(jìn)行改變", this.imageUrl);
this.headFile = file;
this.rules.staffImg[0].required = false;
},
//圖片上傳之前
fileBeforeUpload(file) {
const that = this;
let size = file.size / 1024 / 1024;
if (size >= 1) {
that.$modal.msgError("上傳圖片過(guò)大!請(qǐng)上傳1M以?xún)?nèi)的圖片");
return;
}
const isJpgOrPng =
file.type === "image/jpeg" || file.type === "image/png";
// const isLt1M = file.size / 1024 / 1024 < 1;
const isLt1M = true;
if (!isJpgOrPng) {
this.$message.error("上傳圖片只能是 JPG 或 PNG 格式!");
return false;
}
// 加密
const time = toString(new Date().getTime());
const p = encrypt(time);
this.headers["s"] = p;
this.headers["t"] = time;
return new Promise((resolve) => {
// 小于1M 不壓縮
if (isLt1M) {
resolve(file);
}
// 壓縮到400KB,這里的400就是要壓縮的大小,可自定義
imageConversion.compressAccurately(file, 200).then((res) => {
resolve(res);
});
// compressAccurately有多個(gè)參數(shù)時(shí)傳入對(duì)象
imageConversion
.compressAccurately(file, {
size: 1024, //圖片大小壓縮到1024kb
width: 1280, //寬度壓縮到1280
})
.then((res) => {
resolve(res);
});
});
},
//照片上傳成功
fileUploadSuccess(res, file) {
this.form.staffImg = res.data.dbPath;
this.imageUrl = URL.createObjectURL(file.raw);
this.rules.staffImg[0].required = false;
},原理就是寫(xiě)一個(gè)假的紅色*號(hào),每次點(diǎn)擊查看的時(shí)候執(zhí)行 this.rules.staffImg[0].required = false;
關(guān)閉原有的非空驗(yàn)證,打開(kāi)假的紅色*號(hào)
表單重置的時(shí)候執(zhí)行this.rules.staffImg[0].required = true;
上傳文件改變后執(zhí)行this.rules.staffImg[0].required = false;
圖片上傳成功后執(zhí)行this.rules.staffImg[0].required = false;
到此這篇關(guān)于vue elementUI 上傳非空驗(yàn)證的文章就介紹到這了,更多相關(guān)vue elementUI 非空驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue+elementUI實(shí)現(xiàn)表單和圖片上傳及驗(yàn)證功能示例
- vue elementUI實(shí)現(xiàn)自定義正則規(guī)則驗(yàn)證
- vue+elementUI動(dòng)態(tài)增加表單項(xiàng)并添加驗(yàn)證的代碼詳解
- vue+ElementUI 關(guān)閉對(duì)話(huà)框清空驗(yàn)證,清除form表單的操作
- vue elementUI 表單嵌套驗(yàn)證的實(shí)例代碼
- vue+elementui 對(duì)話(huà)框取消 表單驗(yàn)證重置示例
- vue+elementUI 復(fù)雜表單的驗(yàn)證、數(shù)據(jù)提交方案問(wèn)題
- vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼詳解
相關(guān)文章
axios全局注冊(cè),設(shè)置token,以及全局設(shè)置url請(qǐng)求網(wǎng)段的方法
今天小編就為大家分享一篇axios全局注冊(cè),設(shè)置token,以及全局設(shè)置url請(qǐng)求網(wǎng)段的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
elementUI el-input 只能輸入正整數(shù)驗(yàn)證的操作方法
這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗(yàn)證,本文給大家詳細(xì)講解對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
vue數(shù)組對(duì)象排序的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue數(shù)組對(duì)象排序的實(shí)現(xiàn)代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-06-06
詳解vite+ts快速搭建vue3項(xiàng)目以及介紹相關(guān)特性
這篇文章主要介紹了vite+ts快速搭建vue3項(xiàng)目以及介紹相關(guān)特性,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
VUE3中實(shí)現(xiàn)拖拽與縮放自定義看板vue-grid-layout詳解
想實(shí)現(xiàn)桌面自由拖拽布局的效果,找到了vue-grid-layout柵格布局插件,可以完美解決,下面這篇文章主要給大家介紹了關(guān)于VUE3中實(shí)現(xiàn)拖拽與縮放自定義看板vue-grid-layout的相關(guān)資料,需要的朋友可以參考下2023-02-02
在VUE中使用lodash的debounce和throttle操作
這篇文章主要介紹了在VUE中使用lodash的debounce和throttle操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11

