首页 >> js开发 >> js详解element上传组件before-remove钩子问题解决js大全
js详解element上传组件before-remove钩子问题解决js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
应公司业务要求已上传文件删除前提醒确认代码如下
if(file && file.status === "success"){
return this.$confirm('此操作将永久删除该文件, 是否继续?', '系统提示',{
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning',
center: true
}).then(() => {
this.$message({
type: 'success',
message: '删除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除!'
});
reject(false);
});
};
if(file && file.status === "success"){
return this.$confirm('此操作将永久删除该文件, 是否继续?', '系统提示',{
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning',
center: true
}).then(() => {
this.$message({
type: 'success',
message: '删除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除!'
});
reject(false);
});
};确认会直接调用on-remove方法具体业务代码如下
if (file && file.status==="success") {
this.$axios.delete("url" + data);
}
if (file && file.status==="success") {
this.$axios.delete("url" + data);
}
下面是 before-upload 上传文件前的钩子,在遇到大于10M的文件时,我们返回false
//图片上传前钩子
beforeUpload(file) {
this.loading = true;
const isLt2M = file.size / 1024 / 1024 < 10;
if (!isLt2M) {
this.loading = false;
this.$message.error("单个附件大小不能超过 10MB!");
}
return isLt2M;
// return false;
}
//图片上传前钩子
beforeUpload(file) {
this.loading = true;
const isLt2M = file.size / 1024 / 1024 < 10;
if (!isLt2M) {
this.loading = false;
this.$message.error("单个附件大小不能超过 10MB!");
}
return isLt2M;
// return false;
}
但是这时会出现自动调用before-remove on-remove钩子其实此时我们根本没有上传文件,所以也不会需要删除操作,然后我的代码就报错了。解决办法如下:
//删除图片
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"
});
}
});
}
},
//删除图片
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"
});
}
});
}
},
把不需要执行的代码放入判断内。
if (file && file.status==="success") {
}
if (file && file.status==="success") {
}
if(file && file.status === "success"){
return this.$confirm('此操作将永久删除该文件, 是否继续?', '系统提示',{
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning',
center: true
}).then(() => {
this.$message({
type: 'success',
message: '删除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除!'
});
reject(false);
});
};
if(file && file.status === "success"){
return this.$confirm('此操作将永久删除该文件, 是否继续?', '系统提示',{
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning',
center: true
}).then(() => {
this.$message({
type: 'success',
message: '删除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除!'
});
reject(false);
});
};确认会直接调用on-remove方法具体业务代码如下
if (file && file.status==="success") {
this.$axios.delete("url" + data);
}
if (file && file.status==="success") {
this.$axios.delete("url" + data);
}
下面是 before-upload 上传文件前的钩子,在遇到大于10M的文件时,我们返回false
//图片上传前钩子
beforeUpload(file) {
this.loading = true;
const isLt2M = file.size / 1024 / 1024 < 10;
if (!isLt2M) {
this.loading = false;
this.$message.error("单个附件大小不能超过 10MB!");
}
return isLt2M;
// return false;
}
//图片上传前钩子
beforeUpload(file) {
this.loading = true;
const isLt2M = file.size / 1024 / 1024 < 10;
if (!isLt2M) {
this.loading = false;
this.$message.error("单个附件大小不能超过 10MB!");
}
return isLt2M;
// return false;
}
但是这时会出现自动调用before-remove on-remove钩子其实此时我们根本没有上传文件,所以也不会需要删除操作,然后我的代码就报错了。解决办法如下:
//删除图片
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"
});
}
});
}
},
//删除图片
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"
});
}
});
}
},
把不需要执行的代码放入判断内。
if (file && file.status==="success") {
}
if (file && file.status==="success") {
}