vue中element 上傳功能的實(shí)現(xiàn)思路
element 的上傳功能
最近有個(gè)需求,需要在上傳文件前,可以進(jìn)行彈窗控制是否上傳 upload
看完文檔后,感覺有兩種思路可以實(shí)現(xiàn)
before-upload
auto-upload, on-change
before-upload
初始代碼
// template <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> // javscript data() { return { imageUrl: "" }; }, methods: { handleAvatarSuccess(res, file) { this.imageUrl = URL.createObjectURL(file.raw); }, beforeAvatarUpload(file) { // 文件類型進(jìn)行判斷 const isJPG = file.type === "image/jpeg"; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error("上傳頭像圖片只能是 JPG 格式!"); } if (!isLt2M) { this.$message.error("上傳頭像圖片大小不能超過 2MB!"); } return isJPG && isLt2M; } }
初始效果圖
考慮在before-upload中進(jìn)行彈窗后, return false | reject() 即可
修改代碼
由于 this.$confirm 是異步操作,因而需要等待其結(jié)果才能進(jìn)行下一步操作
async beforeAvatarUpload(file) { const isSubmit = await this.$confirm('此操作將上傳文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }).then(() => { return true }).catch(() => { return false }); console.log(isSubmit) return isSubmit; }
確認(rèn)提交和取消提交 ==> 結(jié)果卻一樣
確認(rèn)提交
取消提交
結(jié)果卻不可以,因而考慮另一種思路了, before-upload 只是進(jìn)行判斷文件是否適合,從而是否上否上傳到服務(wù)器,而不是用來等待用戶進(jìn)行操作使用的
手動(dòng)上傳
auto-upload on-change // template <el-upload ref="upload" class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :limit="1" :auto-upload="false" :on-change="handleChange" :show-file-list="true" :file-list="fileList" :on-error="handleError" :on-success="handleSuccess"> <el-button size="small" type="primary">點(diǎn)擊上傳</el-button> </el-upload> // js data() { return { fileList: [ ], bool: true } }, methods: { handleRemove(file, fileList) { console.log(file, fileList); }, handlePreview(file) { console.log(file); }, handleError(err, file) { alert('失敗') this.fileList = [] }, handleSuccess(res, file) { alert('成功') this.fileList = [] }, handleExceed(files, fileList) {}, async handleChange() { const isSubmit = await this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }).then(() => { return false }).catch(() => { return true }); if (isSubmit) { this.$refs.upload.submit() } else { this.fileList = [] } } }
確認(rèn)提交
取消提交
此方法可行,現(xiàn)在就是控制因?yàn)槲募顟B(tài)改變而導(dǎo)致兩次彈窗, 修改如下
文件狀態(tài)變更 不是成功就是失敗,因而在成功失敗的函數(shù)進(jìn)行控制即可
添加flag標(biāo)識(shí)進(jìn)行控制彈窗即可
data() { return { isConfirm: true } } async handleChange() { if (!this.isConfirm) { this.isConfirm = true return } const bo = await this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }).then(() => { return false }).catch(() => { return true }) if (bo) { this.$refs.upload.submit() this.isConfirm = false } else { this.fileList = [] } }
修改后便可以了,只是注意 在 on-error 和 on-succes 中注意清空 fileList = [] ,這樣還可以重新添加文件
確定上傳
取消上傳
總結(jié)
以上所述是小編給大家介紹的vue中element 的上傳功能的實(shí)現(xiàn)思路,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
el-element中el-table表格嵌套el-select實(shí)現(xiàn)動(dòng)態(tài)選擇對(duì)應(yīng)值功能
這篇文章主要給大家介紹了關(guān)于el-element中el-table表格嵌套el-select實(shí)現(xiàn)動(dòng)態(tài)選擇對(duì)應(yīng)值功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01基于Vue技術(shù)實(shí)現(xiàn)遞歸組件的方法
這篇文章主要為大家詳細(xì)介紹了基于Vue技術(shù)實(shí)現(xiàn)遞歸組件的方法 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Vue.js實(shí)現(xiàn)點(diǎn)擊左右按鈕圖片切換
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)點(diǎn)擊左右按鈕圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07element-ui?table表格控件實(shí)現(xiàn)單選功能代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于element-ui?table表格控件實(shí)現(xiàn)單選功能的相關(guān)資料,單選框是指在?Element?UI?的表格組件中,可以通過單選框來選擇一行數(shù)據(jù)。用戶只能選擇一行數(shù)據(jù),而不能同時(shí)選擇多行,需要的朋友可以參考下2023-09-09vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能
這篇文章主要介紹了vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Vue3+element實(shí)現(xiàn)表格數(shù)據(jù)導(dǎo)出
這篇文章主要為大家學(xué)習(xí)介紹了Vue3如何利用element實(shí)現(xiàn)表格數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-07-07詳解基于webpack和vue.js搭建開發(fā)環(huán)境
本篇文章主要介紹了詳解基于webpack和vue.js搭建開發(fā)環(huán)境 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04