el-upload實現多個圖片上傳
阿新 • • 發佈:2021-12-08
遇到的問題:上傳多個圖片時,會出現閃動的問題。
1.http-request 檔案上傳時的操作;
2. action 由於官方文件為必需屬性,所以我們這裡加上它,但它的值可以隨便寫;
3.before-upload 檔案上傳之前,對上傳的檔案進行限制(根據業務需求來);
4. limit 限制上傳檔案的個數;
5.on-exceed當上傳的檔案超出限制時的操作;
6.file-list 上傳的檔案列表,建議根據官方文件中的格式來;
[{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]
其他詳細屬性見官方文件:https://element.eleme.cn/#/zh-CN/component/upload
<el-upload action="" accept="image/*" list-type="picture-card" :http-request="uploadRequest" :on-remove="handleRemove" :on-exceed="handleExceed" :limit="5" :file-list="list" :before-upload="beforeUpload" > <i class="el-icon-plus"></i> <div slot="file" slot-scope="{ file }" v-loading="imgloading"> <img class="el-upload-list__item-thumbnail" style="width: 100%; height: 100%" :src="file.url" alt="" /> <span class="el-upload-list__item-actions"> <span class="el-upload-list__item-delete" @click="handleRemove(file)" > <i class="el-icon-delete"></i> </span> </span> </div> </el-upload>
在上傳圖片時,新增唯一標識,可以避免出現閃動的問題。
uploadRequest(data) { const form = new FormData(); form.append("file", data.file); upload(form).then((response) => {this.list.push({ uid: data.file.uid, name: data.file.name, url: process.env.VUE_APP_Preview_PATH + response.data.fileUrl, }); }).catch(()=>{ }); },