el-upload 在弹框中上传组件再次打开会闪现之前的文件

44 篇文章 0 订阅
本文介绍了如何使用Element UI的el-upload组件结合CSS样式,实现头像上传限制、显示尺寸调整以及上传状态管理。重点讲解了`.diaForm`中针对`.avatar`和`.limit`的选择器,以及上传前验证和事件处理方法。
摘要由CSDN通过智能技术生成
.diaForm{
  .el-form-item {
    &.limit {
      .el-upload--picture-card {
        display: none;
      }
    }
    &.avatar {
      .el-upload--picture-card {
        width: 140px;
        height: 140px;
        line-height: 140px;
      }
      .el-upload-list__item {
        width: 140px;
        height: 140px;
      }
    }
    .el-upload-list__item {
      transition: none !important;
    }
    ::v-deep .el-upload-list__item { transition: none !important;}
    .el-upload-list__item.is-ready {
      display: none;
    }
  }
}
<el-form-item
  prop="avatar"
  label="头像:"
  class="avatar"
  :class="{limit: hideUploadIcon}">
  <el-upload
    list-type="picture-card"
    :limit="1"
    :action="imageUrl"
    :file-list="avatarList"
    :before-upload="beforeImageUpload"
    :on-success="uploadAvatarSuccess"
    :on-change="handleAvatarChange"
    :on-remove="handleAvatarRemove">
    <i class="el-icon-plus avatar-uploader-icon"></i>
  </el-upload>
</el-form-item>

 hideUploadIcon: false,
 avatarList: [],
 imageUrl: process.env.VUE_APP_BASE_API + 'api/Oss/UploadFile',

// 详情方法
this.avatarList = res.response.avatar ? [{url: res.response.avatar}] : []
this.hideUploadIcon = this.avatarList.length > 0

beforeImageUpload(file) {
  const isImage = file.type.includes('image/');
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isImage) {
    this.$message.error('只能上传图片文件!');
  }
  // if (!isLt2M) {
  //   this.$message.error('上传头像图片大小不能超过 2MB!');
  // }
  return isImage
},

// 上传图
uploadAvatarSuccess(res) {
  this.$set(this.formData, 'avatar', res.response)
},
// 删除图
handleAvatarRemove(file, fileList) {
  this.hideUploadIcon = fileList.length >= 1
  this.$set(this.formData, 'avatar', '')
},
// 监听图的上传事件
handleAvatarChange(file, fileList) {
  this.hideUploadIcon = fileList.length >= 1
},

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值