elementUI上传多张图片回显在编辑( file-list )

时间:2019-12-09 18:06:17   收藏:0   阅读:4002

总有一天你会明白,委屈要自己消化,故事不用逢人就讲。

 

结构:
<el-form-item style="width: 730px;"> <span slot="label"><b style="color:#fff">*</b>头像</span> <el-upload :action="actionPath" list-type="picture-card" :on-remove="handleRemove" :on-change="handleChange" :data="postData" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :file-list="photoList" :class="{ hide: hideUpload }" > <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="formCustom.dialogVisible" style="margin-left:20px"> <img width="100%" :src="formCustom.headPortrait" /> </el-dialog> </el-form-item>

 

data:
actionPath: "https://upload.qiniup.com", //上传到服务器的路径
postData: { token:"生成的token" },
photoList: [],
headPortrait:[]//传给后端

触发的事件:

    // 显示对应数据
    lookList() {
      const req = {id: this.$route.query.id};
      gymnasiumLookVue(req).then(res => {
        if (res.data.code == "200") {
          this.introductionListLook = res.data.data.rows;
          this.list = this.introductionListLook.map(item => {
            const _this = this.formCustom;
            // 回显图片 file-list的值是数组,数组中要有url属性存放图片路径
            item.poster.map(itemImg => {
              let imageUrl = itemImg;
              this.photoList.push({
                url: imageUrl
              });
              this.formCustom.headPortrait = [];
              for (let i = 0; i < this.photoList.length; i++) {
                if (this.photoList[i].url) {
              //将已有的图片push到传给后端的数组中并初始化
            this.formCustom.headPortrait.push(this.photoList[i].url);
                }
              }
              // 回显时如果图片 >=8张隐藏上传按钮
              if (this.photoList.length >= 8) {
                this.hideUpload = true;
              } else {
                this.hideUpload = false;
              }
            });
          });
        } else {
          return false;
        }
      });
    }        
上传成功回调:
handleAvatarSuccess(res) {
      var qiHttp = "https://model.bunnyfit.cn/";
     //拿到所有图片
      this.formCustom.headPortrait.push(qiHttp + res.key);
},
change事件
handleChange(fileList) {
      //如果图片大于等于8张,隐藏上传按钮
      if (this.formCustom.headPortrait.length >= 8) {
        this.hideUpload = true;
      }
}
删除成功的回调
handleRemove(file, fileList) {
     //显示上传按钮
      if (this.formCustom.headPortrait.length <= 8) {
        this.hideUpload = false;
      }
      //删除已经选中的对应图片
      let currentIdIndex = this.formCustom.headPortrait.findIndex(
        item => item == file.url
      );
      if (currentIdIndex != -1) {
        this.formCustom.headPortrait.splice(currentIdIndex, 1);
      }
}

 

 

 

 

原文:https://www.cnblogs.com/home-/p/12012431.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!