詳解element上傳組件before-remove鉤子問題解決
應(yīng)公司業(yè)務(wù)要求已上傳文件刪除前提醒確認(rèn)代碼如下
if(file && file.status === "success"){
return this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '系統(tǒng)提示',{
confirmButtonText: '確認(rèn)',
cancelButtonText: '取消',
type: 'warning',
center: true
}).then(() => {
this.$message({
type: 'success',
message: '刪除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消刪除!'
});
reject(false);
});
};
確認(rèn)會(huì)直接調(diào)用on-remove方法具體業(yè)務(wù)代碼如下
if (file && file.status==="success") {
this.$axios.delete("url" + data);
}
下面是 before-upload 上傳文件前的鉤子,在遇到大于10M的文件時(shí),我們返回false
//圖片上傳前鉤子
beforeUpload(file) {
this.loading = true;
const isLt2M = file.size / 1024 / 1024 < 10;
if (!isLt2M) {
this.loading = false;
this.$message.error("單個(gè)附件大小不能超過 10MB!");
}
return isLt2M;
// return false;
}
但是這時(shí)會(huì)出現(xiàn)自動(dòng)調(diào)用before-remove on-remove鉤子
其實(shí)此時(shí)我們根本沒有上傳文件,所以也不會(huì)需要?jiǎng)h除操作,然后我的代碼就報(bào)錯(cuò)了。
解決辦法如下:
//刪除圖片
beforeRemove(file, fileList) {
let a = true;
if (file && file.status==="success") {
a = this.$confirm(`確定移除 ${ file.name }?`);
}
return a;
},
//刪除圖片
handleRemove(file, fileList) {
if (file && file.status==="success") {
this.$axios.delete("accessory/one/" + file.response.id).then(resp => {
if (resp.status == 200) {
this.$message({
message: "刪除成功",
type: "success"
});
}
});
}
},
把不需要執(zhí)行的代碼放入判斷內(nèi)。
if (file && file.status==="success") {
}
到此這篇關(guān)于詳解element上傳組件before-remove鉤子問題解決的文章就介紹到這了,更多相關(guān)element上傳組件before-remove鉤子內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vuex中如何getters動(dòng)態(tài)獲取state的值
這篇文章主要介紹了Vuex中如何getters動(dòng)態(tài)獲取state的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目的實(shí)例代碼
這篇文章主要介紹了vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目,填寫數(shù)據(jù)庫連接信息和查詢語句,即可展示查詢到的數(shù)據(jù),需要的朋友可以參考下2022-11-11
在Vue3中實(shí)現(xiàn)子組件向父組件傳遞數(shù)據(jù)的代碼示例
Vue3作為目前最熱門的前端框架之一,以其輕量化、易用性及性能優(yōu)勢(shì)吸引了大量開發(fā)者,在開發(fā)過程中,不可避免地需要在組件之間傳遞數(shù)據(jù),本文將詳細(xì)講解在Vue3中如何實(shí)現(xiàn)子組件向父組件傳遞數(shù)據(jù),并通過具體示例代碼使概念更加清晰2024-07-07

