elementUi upload上传达到limit后隐藏upload图标

文章讲述了在项目中使用elementUi的el-upload组件来实现上传控件,限制最多只能上传3张图片。当达到限制时,自动隐藏上传按钮;删除照片后,按钮重新显示。为解决删除动画导致的显示问题,采取了在删除操作中加入1秒延迟的方法来同步更新界面状态。
部署运行你感兴趣的模型镜像

如题所述,我们项目要求上传控件最大只允许上传3张。当数量达到的时候,自动隐藏上传按钮控件。而当点击删除之前的照片后,又重新显示上传按钮控件。

这里我们选用了饿了么的elementUi里的el-upload控件作为基础,再添加我们的需求。

主要的templete如下

<el-upload
          ref="uploadRef"
          action="fakeaction"
          :http-request="uploadFile"
          :class="attachmentArray.length >= 3 ? 'hide' : 'show'"
          list-type="picture-card"
          :before-upload="beforeAttachmentUpload"
          :auto-upload="true"
          :limit="limit"
          :on-exceed="handleExceed"
          accept=".jpg,.png,.jpeg"
          >
            <div slot="file" class="upload-item" slot-scope="{file}">
                <div class="upload-img-wrapper">
                    <img class="el-upload-list__item-thumbnail" :src="file.url" @click="handlePictureCardPreview(file)">
                </div>
                <i @click="handleRemove(file)" class="delete-icon"></i>
            </div>
        </el-upload>

而对应的style如下

.hide   {
  position: relative;
  float: left;
  padding-top: 20px;
  padding-left: 23px;
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }

    .el-upload-list__item {
      border: 0;
      border-radius: 0;
      margin:0 30px 0 0;
    }
  }
}

但是光这样子设置呢,会有些许瑕疵,因为控件本身有删除动画时长,进而使得先出现了第四个按钮,再等删除动画完成后,重新显示了三个控件。

所以,我们就必须在删除操作里,给个响应时长。

 handleRemove(file) {
      // console.log(file);
      const index = this.$refs.uploadRef.uploadFiles.findIndex(e => e.uid === file.uid);
      this.$refs.uploadRef.uploadFiles.splice(index, 1);
      setTimeout(() => {
        this.attachmentArray.splice(index,1);
      }, 1000);
      // console.log(this.attachmentArray,"attachmentArray");
    },

这样子才能完美显示/隐藏按钮。

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

仅给定的引用中未提及 elementui upload 组件上传图片后不显示的解决办法,但可以从一般情况分析可能的解决思路。 ### 1. 检查图片 URL 是否正确 上传成功后,服务器返回的图片 URL 可能存在问题。需要确保返回的 URL 是有效的,并且可以在浏览器中直接访问。可以在 `handleSwiperSuccess` 钩子函数中打印返回的 URL 进行检查: ```vue <template> <el-upload :before-upload="beforePicUpload" :on-remove="handleSwiperRemove" :on-success="handleSwiperSuccess" action="/api/upload/pic" class="avatar-uploader" list-type="picture-card" multiple ref="SwiperlUpload" > <el-button size="small" type="primary">点击上传</el-button> </el-upload> </template> <script> export default { methods: { handleSwiperSuccess(response, file, fileList) { console.log('上传成功返回的图片 URL:', response.url); // 检查返回的 URL // 可以在这里更新图片显示 }, beforePicUpload(file) { // 上传前的判断 return true; }, handleSwiperRemove(file, fileList) { // 文件删除的操作 } } }; </script> ``` ### 2. 检查图片显示绑定是否正确 如果使用 `el-upload` 的 `list-type="picture-card"` 显示图片,需要确保图片列表绑定正确。可以在 `handleSwiperSuccess` 中更新图片列表: ```vue <template> <el-upload :before-upload="beforePicUpload" :on-remove="handleSwiperRemove" :on-success="handleSwiperSuccess" action="/api/upload/pic" class="avatar-uploader" list-type="picture-card" multiple ref="SwiperlUpload" :file-list="fileList" > <el-button size="small" type="primary">点击上传</el-button> </el-upload> </template> <script> export default { data() { return { fileList: [] }; }, methods: { handleSwiperSuccess(response, file, fileList) { this.fileList.push({ url: response.url }); // 更新图片列表 }, beforePicUpload(file) { return true; }, handleSwiperRemove(file, fileList) { // 从 fileList 中移除文件 const index = fileList.indexOf(file); if (index !== -1) { fileList.splice(index, 1); } } } }; </script> ``` ### 3. 检查跨域问题 如果图片服务器和前端应用不在同一个域名下,可能会存在跨域问题。需要确保服务器端配置了正确的 CORS 策略,允许前端域名访问图片资源。 ### 4. 检查图片加载错误 可以使用 `@error` 事件监听图片加载错误: ```vue <template> <el-upload :before-upload="beforePicUpload" :on-remove="handleSwiperRemove" :on-success="handleSwiperSuccess" action="/api/upload/pic" class="avatar-uploader" list-type="picture-card" multiple ref="SwiperlUpload" :file-list="fileList" > <el-button size="small" type="primary">点击上传</el-button> <template #file="scope"> <img :src="scope.file.url" alt="" @error="handleImageError(scope.file)" /> </template> </el-upload> </template> <script> export default { data() { return { fileList: [] }; }, methods: { handleSwiperSuccess(response, file, fileList) { this.fileList.push({ url: response.url }); }, beforePicUpload(file) { return true; }, handleSwiperRemove(file, fileList) { const index = fileList.indexOf(file); if (index !== -1) { fileList.splice(index, 1); } }, handleImageError(file) { console.log('图片加载失败:', file.url); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值