99八十一 2018-12-12 05:36 采纳率: 0%
浏览 11719

vue+element列表中每一项使用一次el-upload后,单独删除该项的数据时,列表中所有的el-upload的图片被清空

el-upload组件代码:


<template>

  <el-upload
    class="avatar-uploader"
    :class="{avatarUploaderMimi:size == 'mini', disabled:fileList.length >= (imageCount?imageCount:1)}"
    :action="uploadUrl"
    :file-list="fileList"
    :ref="refUpload"
    name="file"
    list-type="picture-card"
    :on-change="handleChange"
    :on-remove="handleRemove">
    <div class="avatar-uploader-center">
      <i class="el-icon-plus avatar-uploader-icon"></i>

      <div class="el-upload__tip" v-show="text">{{text}}</div>
    </div>

  </el-upload>

</template> 

<script>

import Config from '@/common/js/config';

/**
 * @param imageUrl : [{url:''}]
 * @param imageCount : 上传数量
 * @param text
 * @returns arr
 */

export default {
  name: 'uploadImg',

  props: ['imageUrl', 'imageCount', 'text' , 'size','refUpload'],

  data() {
    return {
      uploadUrl:'',
      fileList:[],
    };
  },

  created() {
    this.uploadUrl = Config.uploadUrl;
    if(this.imageUrl == '' || this.imageUrl == undefined || this.imageUrl == null){
      this.fileList = [];
    }
    if(this.imageUrl && this.imageUrl.length > 0){
      if(this.imageUrl[0].url == '' || this.imageUrl[0].url == null || this.imageUrl[0].url == undefined){
        this.fileList = [];
      }else {
        this.fileList = this.imageUrl;
      }
    }

  },

  methods: {
    imgRemove(){
      console.log(this.$refs);
      this.$refs[this.refUpload].clearFiles();
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },

    handleRemove(file, fileList) {
      this.fileList = fileList;
    },

    beforeAvatarUpload(file) {

      const isJPEG = file.type === 'image/jpeg';
      const isJPG = file.type === 'image/jpg';
      const isPNG = file.type === 'image/png';
      const isGIF = file.type === 'image/gif';
      const isLt2M = file.size / 1024  < 512;

      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过512k!');
      }
       return (isJPG || isJPEG || isPNG || isGIF) && isLt2M;
    },


    // 返回图片在服务器的URL
    getUploadImage(){
      let imgUrl;
      this.fileList.map(item => {
        if(item.response){
          imgUrl = item.response.data;
        }else {
          imgUrl = item.url;
        }
      });
      return imgUrl;
    },


  }

}

</script>

<style>

  .avatar-uploader .el-upload {
    line-height: inherit;
  }

  .avatar-uploader .el-icon-close-tip{
    opacity: 0;
  }

  .avatar-uploader .el-upload,
  .avatar-uploader .el-upload-list__item{
    width: 100px !important;;
    height: 100px !important;;
  }

  .avatarUploaderMimi .el-upload,
  .avatarUploaderMimi .el-upload-list__item{
    width: 50px !important;;
    height: 50px !important;;
  }


  .avatar-uploader.disabled .el-upload--picture-card {
    display: none;
  }

  .avatar-uploader-center{
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 100%;
  }

</style>


父组件调用代码

<div class="input-wrap" v-for="(item,index) in skuList" :key="RndNum(8)">
                  <upload-img ref="skuImgList" :ref-upload="'refUpload' + RndNum(4)" :image-count="uploadCount" size="mini" class="sku-img"></upload-img>

<div class="input-wrap">
<span style="color: blue;cursor: pointer;" @click="removeSku(item,index)">删除</span>
</div>
</div>


script=============
removeSku(item, index) {
        this.$refs.skuImgList[index].imgRemove();
        this.skuList.splice(index, 1);
      },

删除前===========================

删除前

删除后============================

删除后

我尝试过给子组件加v-if显示隐藏,不行,也尝试过把el-upload拿出来直接用然后把列表内每一项加一个fileList来使用也不行,实在是搞不懂了。
求大神指教!

  • 写回答

2条回答

  • 阿西亚 2020-12-14 14:50
    关注

    请问最后怎么解决了

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?