element-ui圖片上傳組件查看和限制方式
element-ui圖片上傳組件查看和限制
element-ui經(jīng)常遇到圖片上傳,然后編輯查看限制上傳個數(shù)和上傳提示
- 1、limit上傳個數(shù)限制配合on-exceed屬性使用
- 2、上傳之前提示圖片大小限制屬性before-upload
- 3、上傳帶參數(shù)data
- 4、上傳成功on-success和移除on-remove
- 5、file-list屬性展示的列表數(shù)組,查看也是設(shè)置這個
html的代碼
<el-upload? ?? ?:limit="1"? ?? ?:on-exceed="config.exceed" ref="upload" class="upload-demo"? ?? ?:before-upload="config.onBeforeUpload"? ?? ?:on-success="config.handleAvatarSuccess"? ?? ?:data="config.dataParam"? ?? ?:action="config.actionUrl" ?? ? :on-remove="config.handleRemove" ?? ? ?:file-list="config.fileList2" //保存當(dāng)前圖片對象數(shù)組 ?? ? ?:multiple="false"? ?? ? ?:show-file-list="true"? ?? ? ?list-type="picture"> ? ? ? ? ? ? <el-button size="small" type="primary">點(diǎn)擊上傳</el-button> ? </el-upload>
配置信息js 代碼
config: { ? ? ? ? dataParam: { ? ? ? ? ? bizType: 3, ? ? ? ? ? useType: 2 ? ? ? ? }, ? ? ? ? actionUrl: 'api/base-fdfs/fdfs/public/upload', ? ? ? ? exceed:()=>{ ? ? ? ? ? ? this.$alert('請刪除后再上傳', '異常提示', { ? ? ? ? ? ? ? confirmButtonText: '確定', ? ? ? ? ? ? ? type: 'warning' ? ? ? ? ? ? }); ? ? ? ? }, ? ? ? ? handleRemove: (file,fileList) => { ? ? ? ? ? this.form.picture='' ? ? ? ? ? this.config.fileList2=[] //刪除此數(shù)組內(nèi)的圖片對象 ? ? ? ? }, ? ? ? ? onBeforeUpload: (file) => { ? ? ? ? ? const isIMAGE = ((file.type === 'image/jpeg') || (file.type === 'image/png') || (file.type === 'image/bmp')); ? ? ? ? ? if (!isIMAGE) { ? ? ? ? ? ? this.$alert('上傳文件只能是圖片格式(jpeg/png/bmp)', '異常提示', { ? ? ? ? ? ? ? confirmButtonText: '確定', ? ? ? ? ? ? ? type: 'warning' ? ? ? ? ? ? }); ? ? ? ? ? ? return false ? ? ? ? ? } ? ? ? ? ? if(file.size/1024/1024>5){ ? ? ? ? ? ? console.log(file.size) ? ? ? ? ? ? this.$alert('圖片不能大于5M', '異常提示', { ? ? ? ? ? ? ? confirmButtonText: '確定', ? ? ? ? ? ? ? type: 'warning' ? ? ? ? ? ? }); ? ? ? ? ? ? return false ? ? ? ? ? } ? ? ? ? ? return true ? ? ? ? }, ? ? ? ? handleAvatarSuccess: (res, file) => { ? ? ? ? ? this.form.repairImages = file.response.data ? ? ? ? }, ? ? ? ? fileList2: [], ? ? ? },
查看的時候還需給file-list數(shù)組設(shè)置,展示圖片
? ? let obj={} ?? ?this.config.fileList2=[] ?? ?this.$set(obj,'name','file'); ?? ?this.$set(obj,'url',response.data.picture); ?? ?this.config.fileList2.push(obj) ?
清除文件列表,上傳組件變初始狀態(tài)
this.$refs.upload.clearFiles() //清除圖片
element-ui限制一張圖片上傳
需求描述
在做圖片上傳組件的時候 有時候需求回要求只能上傳一張圖片
bug:element-ui在上傳完一張圖片后上傳按鈕還會存在 雖然可以用自帶的limt屬性加以限制 但是上傳按鈕依然會存在 且存入數(shù)組的值依然會增加
解決
可以判斷上傳后數(shù)組的length來控制圖片上傳的樣式 讓其上傳一張后隱藏上傳按鈕 達(dá)到控制圖片上傳的數(shù)量需求
<el-form-item label="禮包主圖" prop="goodsimage" :required="true"> <el-upload :class="{ disabled: uploadDisabled }" //這里動態(tài)控制樣式 :limit="1" ref="picUpload" class="pic-div" action="'xxxxxxx'" :with-credentials="true" list-type="picture-card" :on-remove="removePic" :on-change="handleEditChange" :file-list="goodsimage" :on-exceed="beyond" > <div class="el-upload file-upload-container" @click.stop="popFileUpload('0')"> <i class="el-icon-plus"></i> </div> </el-upload> </el-form-item>
// 隱藏圖片上傳按鈕 uploadDisabled() { return this.goodsimage.length > 0 //判斷圖片上傳的數(shù)量動態(tài)控制按鈕隱藏與顯示 },
<style> .disabled .el-upload--picture-card { display: none; } </style>
最終效果
禮包視頻也是如此要求僅限上傳1個視頻 由于是button按鈕 俺就直接使用v-if判斷視頻的length來控制視頻上傳按鈕的顯示與隱藏
<el-button ***v-if="this.goodsvideo.length < 1"*** class="file-upload-btn-container" @click.stop="popFileUpload('1')" size="small" type="primary">點(diǎn)擊上傳</el-button >
上傳視頻后按鈕隱藏
em…
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue.js在編寫過程中出現(xiàn)空格不規(guī)范報錯的問題
下面小編就為大家?guī)硪黄鉀Qvue.js在編寫過程中出現(xiàn)空格不規(guī)范報錯的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09淺談vue在html中出現(xiàn){{}}的原因及解決方式
這篇文章主要介紹了淺談vue在html中出現(xiàn){{}}的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue中實(shí)現(xiàn)methods一個方法調(diào)用另外一個方法
下面小編就為大家分享一篇vue中實(shí)現(xiàn)methods一個方法調(diào)用另外一個方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue2.0與vue3.0及vue與react的區(qū)別及說明
這篇文章主要介紹了vue2.0與vue3.0及vue與react的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue使用elementUI組件實(shí)現(xiàn)分頁效果
分頁在展示數(shù)據(jù)列表的場景肯定是非常多的,一般的項(xiàng)目開發(fā)中,數(shù)據(jù)量特別大,一般都是后端接口直接處理分頁返回,前端直接調(diào)用即可,本文給大家介紹了vue使用elementUI組件實(shí)現(xiàn)分頁效果,需要的朋友可以參考下2023-12-12深入理解與使用keep-alive(配合router-view緩存整個路由頁面)
這篇文章主要介紹了深入理解與使用keep-alive(配合router-view緩存整個路由頁面),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Vue使用Echarts實(shí)現(xiàn)大屏可視化布局示例詳細(xì)講解
這篇文章主要介紹了Vue使用Echarts實(shí)現(xiàn)大屏可視化布局示例,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01