el-upload 的el-icon-upload-success和el-icon-close是怎么切换的
时间: 2025-06-01 17:16:19 浏览: 28
### el-upload 组件中图标切换逻辑
在 `el-upload` 组件中,`el-icon-upload-success` 和 `el-icon-close` 图标的切换逻辑主要依赖于文件上传的状态以及自定义的事件处理函数。以下是详细的实现方式和逻辑:
#### 1. 文件上传成功时显示 `el-icon-upload-success`
当文件上传成功时,可以通过 `on-success` 钩子函数来捕获上传成功的状态,并动态更新组件的状态以显示成功图标。例如:
```javascript
methods: {
handleSuccess(response, file, fileList) {
// 在此方法中,可以将文件标记为已上传成功
file.status = 'success'; // 自定义属性标记文件状态
console.log('File uploaded successfully:', response, file, fileList);
}
}
```
在模板中,可以通过判断文件状态动态切换图标:
```html
<el-upload
:on-success="handleSuccess"
action="https://jsonplaceholder.typicode.com/posts/">
<template #default="{ file }">
<div v-if="file.status === 'success'">
<i class="el-icon-upload-success"></i>
</div>
<div v-else>
<i class="el-icon-close"></i>
</div>
</template>
</el-upload>
```
通过这种方式,可以根据文件的状态动态切换图标[^1]。
#### 2. 文件上传失败或移除时显示 `el-icon-close`
当文件上传失败或被用户手动移除时,可以通过 `on-error` 和 `on-remove` 钩子函数捕获相关事件,并更新组件状态以显示关闭图标。
```javascript
methods: {
handleError(err, file, fileList) {
// 标记文件上传失败
file.status = 'error';
console.error('File upload failed:', err, file, fileList);
},
handleRemove(file, fileList) {
// 移除文件时的操作
console.log('File removed:', file, fileList);
}
}
```
同样,在模板中可以通过文件状态动态切换图标:
```html
<el-upload
:on-error="handleError"
:on-remove="handleRemove"
action="https://jsonplaceholder.typicode.com/posts/">
<template #default="{ file }">
<div v-if="file.status === 'error' || file.status === 'removed'">
<i class="el-icon-close"></i>
</div>
<div v-else>
<i class="el-icon-upload-success"></i>
</div>
</template>
</el-upload>
```
#### 3. 动态绑定类名实现图标切换
如果需要更灵活地控制图标样式,可以使用动态绑定类名的方式。例如:
```html
<el-upload
:on-success="handleSuccess"
:on-error="handleError"
:on-remove="handleRemove"
action="https://jsonplaceholder.typicode.com/posts/">
<template #default="{ file }">
<i :class="getIconClass(file)"></i>
</template>
</el-upload>
```
在方法中定义图标切换逻辑:
```javascript
methods: {
getIconClass(file) {
if (file.status === 'success') {
return 'el-icon-upload-success';
} else if (file.status === 'error' || file.status === 'removed') {
return 'el-icon-close';
} else {
return 'el-icon-loading'; // 默认加载状态
}
}
}
```
#### 4. 注意事项
- 确保在 `on-success` 和 `on-error` 钩子中正确处理文件状态。
- 如果需要区分不同组件上传的文件,可以结合下标 `j` 或唯一标识符来管理文件状态[^1]。
- 在 TypeScript 环境下使用 `el-upload` 时,注意处理 `UploadAjaxError` 对象的嵌套结构,确保正确访问后端返回的数据[^2]。
### 示例代码
以下是一个完整的示例代码:
```html
<el-upload
:on-success="handleSuccess"
:on-error="handleError"
:on-remove="handleRemove"
action="https://jsonplaceholder.typicode.com/posts/">
<template #default="{ file }">
<i :class="getIconClass(file)"></i>
</template>
</el-upload>
<script>
export default {
methods: {
handleSuccess(response, file, fileList) {
file.status = 'success';
},
handleError(err, file, fileList) {
file.status = 'error';
},
handleRemove(file, fileList) {
file.status = 'removed';
},
getIconClass(file) {
if (file.status === 'success') {
return 'el-icon-upload-success';
} else if (file.status === 'error' || file.status === 'removed') {
return 'el-icon-close';
} else {
return 'el-icon-loading';
}
}
}
};
</script>
```
阅读全文
相关推荐










<el-upload ref="uploadBack" list-type="picture-card" :before-upload="field103BeforeUpload" accept=".pdf"
:limit="1" :headers="uploadPdf.headers" :action="uploadPdf.url" :disabled="uploadPdf.isUploading"
:on-success="handlePdfFileSuccess">
<template slot="file" slot-scope="{ file }">
{{ file.name }}
</template>
</el-upload>在上传前判断文件名是否有空格,如果有,就去掉空格然后继续自己上传, field103BeforeUpload(file) {
if (/\s/.test(file.name)) {
const newFile = new File([file], file.name.replace(/\s/g, ''), {
type: file.type,
lastModified: Date.now()
})
this.$nextTick(() => {
this.$refs.uploadBack.uploadFiles = [] // 清空原有文件列表
this.$refs.uploadBack.handleStart(newFile)
})
return false
}
this.loadingTitle = '开始上传'
this.uploadLoading = this.$loading({
lock: true,
text: this.loadingTitle,
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
})
return true
},在上传成功的方法是否也需要更改handlePdfFileSuccess(response, file, fileList) {
console.log('*** file ***', file)
if (response.code === 200) {
this.loadingTitle = '上传成功'
this.uploadLoading.close()
// this.localPdfPth=file.
const newFileObj = {
url: response.data.url,
uid: file.uid, // 保留原始文件的uid
name: file.name // 如果需要,也可以保留文件名
}
this.formData.waitSignUrl.push(newFileObj)
} else {
if (this.loading != null) {
this.loading.close()
}
}
},




