vue element-ui el-upload去除按delete 键可删除提示

这段内容描述了一个使用Element UI的上传组件实现的功能,该组件限制上传一个PDF文件,并在成功上传后执行特定的回调函数。样式设置隐藏了已成功上传文件的焦点状态,确保用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决办法:

<el-upload
class="uploadList"
ref="upload"
:action="baseUrl + '/file/upload'"
:before-upload="beforeUpload"
:on-preview="handlePreview"
:multiple="false"
:headers="{ Authorization: token }"
:limit="1"
:on-remove="handleRemove"
:on-exceed="handleExceed"
:on-success="handleSuccess"
>
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip fontIntail">只能上传一个PDF文件,一个保单对应一个电子保单文件</div>
</el-upload>

.uploadList .el-upload-list__item.is-success:focus:not(:hover){
    display: none !important;
}

### Vue2 Element UI `el-upload` 组件自定义删除按钮 为了在 Vue2 中使用 Element UI 的 `el-upload` 组件时自定义删除按钮的行为,可以通过覆盖默认的上传行为以及监听特定事件来实现。具体来说,通过设置 `:http-request` 属性来自定义上传逻辑,并利用 `before-remove` 或者 `on-remove` 钩子函数控制文件移除前后的操作。 对于隐藏或修改删除图标样式的情况,可以采用 CSS 来调整 `.el-upload-list__item-delete` 类的选择器[^1]: ```css /* 修改删除图标的外观 */ .el-upload-list__item-delete { /* 定义新的样式规则 */ } ``` 如果目标是完全替换原有的删除功能,则可以在模板内添加额外的操作按钮并与之交互。例如,在 `<template>` 标签内部增加一个用于触发自定义删除动作的新按钮,并将其放置于合适的位置上;同时确保该按钮能够访问到当前项的数据以便执行相应的业务逻辑处理。 另外一种方式是在 JavaScript 方法中拦截实际发生的删除请求,从而允许开发者在此基础上加入更多验证或者其他前置条件判断。这通常涉及到对组件实例上的 `$refs` 进行调用来获取列表数据并实施更精细的操作[^3]。 最后值得注意的一点是,当实现了上述任何一项更改之后,仍然要保留原始组件必要的配置选项(比如 `action`),因为这些参数可能会影响整个上传流程的工作机制[^2]。 #### 示例代码展示如何创建带有自定义删除按钮的 `el-upload` 组件 ```html <template> <div class="custom-upload"> <!-- 使用 ref 获取 upload 实例 --> <el-upload ref="upload" action="/your/upload/endpoint" :auto-upload="false" :file-list="fileList" :on-preview="handlePreview" :on-change="handleChange" list-type="picture-card" > <i slot="default" class="el-icon-plus"></i> <div v-if="!isUploading && fileList.length === 0">点击或将文件拖拽到这里</div> <!-- 自定义删除按钮 --> <span v-for="(file, index) in fileList" :key="index" style="position:absolute;top:-8px;right:-8px;z-index:9;" > <el-button @click.prevent.stop="removeFile(file)"> × </el-button> </span> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt=""> </el-dialog> </div> </template> <script> export default { data() { return { dialogImageUrl: '', dialogVisible: false, isUploading: false, fileList: [] }; }, methods: { handleRemove(file, fileList) { // 默认删除回调 console.log('Default remove:', file); }, handleChange(file, fileList) { // 文件状态改变时触发 this.fileList = fileList; }, handlePreview(file) { // 查看图片预览 this.dialogImageUrl = file.url || URL.createObjectURL(file.raw); this.dialogVisible = true; }, removeFile(file) { // 自定义删除方法 const idx = this.fileList.findIndex(f => f.uid === file.uid); if (idx !== -1) { this.$confirm(`确定要删除 ${file.name} 吗?`, '警告', { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' }).then(() => { this.fileList.splice(idx, 1); // 移除指定索引处的对象 this.$message({ message: `${file.name} 已成功删除`, type: 'success' }); }).catch(() => {}); } } } }; </script> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

船长在船上

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值