Element?UI?Upload?組件上傳圖片可刪除、預(yù)覽功能
Element UI Upload 組件 上傳圖片可刪除、預(yù)覽;設(shè)置只允許上傳單張 / 多張圖片的操作
效果



上傳圖片單張圖片,可刪除、預(yù)覽 完整代碼
如果不上傳單張圖片,可以把 :limit=“1” 刪除,把單張圖片操作步驟刪除就是多張圖片上傳咯!
html部分
<el-form-item label="頭像" prop="csAvatar">
<el-upload action="aaa" list-type="picture-card" :class="{ disabled: uploadDisabled }"
:auto-upload="false" :limit="1" ref="upload" :on-change="handleChange" :on-preview="handlePictureCardPreview"
:on-remove="handleRemove" :file-list="fileList">
<i class="el-icon-plus"></i>
</el-upload>
<!-- 預(yù)覽圖片 -->
<el-dialog :visible.sync="dialogVisibleimg" append-to-body>
<img width="100%" :src="dialogImageUrl" alt="" />
</el-dialog>
</el-form-item>javascript
data(){
return {
dialogVisibleimg: false,
dialogImageUrl: "",
formdata: new FormData(),
fileList: [],
addimg: [],
removeimg: [],
ruleForm: {
csAvatar: "",
}
},
methods:{
// 添加活動(dòng)展示照片
handleChange(file, fileList) {
const isJPG =file.raw.type === "image/jpeg" || file.raw.type === "image/png";
const isLt5M = file.size / 1024 / 1024 < 5;
if (!isJPG) {
this.$message.error("上傳頭像圖片只能是 JPG 、png 格式!");
fileList.splice(-1, 1);//移除錯(cuò)誤文件
return false;
}
if (!isLt5M) {
this.$message.error("上傳頭像圖片大小不能超過 5MB!");
fileList.splice(-1, 1);
return false;
}
this.addimg = fileList[0].raw;
this.ruleForm.csAvatar = this.addimg;
},
// 刪除活動(dòng)展示照片
handleRemove(file, fileList) {
console.log(fileList)
this.ruleForm.csAvatar = '';
this.formdata = new FormData();
},
// 活動(dòng)展示照片預(yù)覽
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisibleimg = true;
},
// 渲染編輯獲取id
apply() {
this.fileList = [{ url: "" }];//這里是踩坑的點(diǎn),必須寫
this.fileList[0].url = res.data.csAvatar;//這里是調(diào)用接口獲取到的值 res.data.csAvatar,賦值就可以回顯了
//this.fileList[0].url 做的是單張圖片回顯,多張圖片回顯可以去掉[0]
},
// 提交上傳按鈕
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
this.formdata = new FormData();
if(this.ruleForm.csAvatar){//新增
this.formdata.append("picture", this.ruleForm.csAvatar);//上傳圖片要把參數(shù)和值放在FormData里
//如果有其他參數(shù),也要一并放在this.formdata里
/* 例:
this.formdata.append("csLevel", this.ruleForm.serviceLevel);
this.formdata.append("csMaximum", this.ruleForm.size);
*/
}
//上傳圖片的接口(重要看傳參部分怎么傳參)
CustomerServiceController.add(this, this.formdata)//傳參直接傳this.formdata即可
.then((res) => {
if (res.success == true) {
this.$message.success("新建成功");
} else {
this.$message.error("新建失敗");
}
})
.catch((e) => {});
} else {
console.log("error submit!!");
return false;
}
});
},
},
computed: {
uploadDisabled: function () {
console.log(this.ruleForm.csAvatar)
return this.ruleForm.csAvatar !='';
},
},
}css
.disabled{
.el-upload--picture-card{
display: none;
}
}Element UI Upload 組件 設(shè)置只允許上傳單張圖片的操作
只要在檢測(cè)到上傳列表中已經(jīng)有一張圖片時(shí),就隱藏上傳組件,只展示上傳列表。

那么就可以給整個(gè)上傳組件一個(gè)class名稱,這個(gè)class沒有任何樣式,只是做選擇器用。
比如:disabled
那么就在監(jiān)測(cè)到上傳一張后,隱藏disabled 下面的 el-upload–picture-card

js:
computed: {
uploadDisabled:function() {
return this.imagelist.length >0
},
},css:
.disabled .el-upload--picture-card {
display: none;
}uploadDiseabled 會(huì)根據(jù)上傳列表的長(zhǎng)度動(dòng)態(tài)的給upload的組件添加樣式,進(jìn)而將達(dá)到只上傳一張的效果。然后這個(gè)方法也可以用于限制上傳張數(shù)。
到此這篇關(guān)于Element UI Upload 組件 上傳圖片可刪除、預(yù)覽;設(shè)置只允許上傳單張 / 多張圖片的操作的文章就介紹到這了,更多相關(guān)Element UI Upload 組件上傳圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中通過Vue.extend動(dòng)態(tài)創(chuàng)建實(shí)例的方法
這篇文章主要介紹了Vue中通過Vue.extend動(dòng)態(tài)創(chuàng)建實(shí)例的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
nuxt.js中間件實(shí)現(xiàn)攔截權(quán)限判斷的方法
這篇文章主要介紹了nuxt.js中間件實(shí)現(xiàn)攔截權(quán)限判斷的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Nuxt.js nuxt-link與router-link的區(qū)別說明
這篇文章主要介紹了Nuxt.js nuxt-link與router-link的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue3.0實(shí)現(xiàn)移動(dòng)端電子簽名組件
這篇文章主要為大家詳細(xì)介紹了vue3.0實(shí)現(xiàn)移動(dòng)端電子簽名組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
vue實(shí)現(xiàn)tagsview多頁(yè)簽導(dǎo)航功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)tagsview多頁(yè)簽導(dǎo)航功能,本文梳理了一下vue-element-admin項(xiàng)目實(shí)現(xiàn)多頁(yè)簽功能的整體步驟,需要的朋友可以參考下2022-08-08

