elementPlus给el-upload加loading状态
时间: 2023-11-02 18:06:08 浏览: 824
elemetUi 组件--el-upload实现上传Excel文件的实例
5星 · 资源好评率100%
为el-upload添加loading状态,可以通过以下步骤实现:
1. 在el-upload组件中添加一个loading属性,用于控制loading状态的显示和隐藏。
```html
<el-upload
class="upload-demo"
action="/upload"
:show-file-list="false"
:loading="uploading"
:on-success="handleSuccess"
:on-error="handleError">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button size="small" type="success" @click="submitUpload">上传到服务器</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
```
2. 在Vue组件中定义uploading变量,并在上传前将其设置为true,上传成功或失败后将其设置为false。
```javascript
export default {
data() {
return {
uploading: false
};
},
methods: {
submitUpload() {
this.uploading = true;
this.$refs.upload.submit();
},
handleSuccess(res, file) {
this.uploading = false;
this.$message({
message: '上传成功',
type: 'success'
});
},
handleError(err, file) {
this.uploading = false;
this.$message.error('上传失败');
}
}
}
```
3. 在CSS中定义loading样式,用于显示loading状态。
```css
.el-upload.is-uploading .el-upload__input {
display: none;
}
.el-upload.is-uploading .el-upload__tip {
display: none;
}
.el-upload.is-uploading .el-button {
pointer-events: none;
opacity: 0.6;
}
.el-upload.is-uploading .el-button.loading:before {
content: '';
position: absolute;
top: calc(50% - 10px);
left: calc(50% - 10px);
width: 20px;
height: 20px;
border-radius: 50%;
border: 2px solid #fff;
border-top-color: #409EFF;
animation: upload-loading 0.6s linear infinite;
}
@keyframes upload-loading {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
```
这样就可以为el-upload添加loading状态了,上传文件时会显示loading动画。
阅读全文