andv vue 实现多张图片上传

1、提示

注意::: 便利出来的数组 点击保存需要 把 双引号去掉

javascript 复制代码
 this.formData.image = this.imageUrlList.filter((image) => image !== '')

注意::: 回显的时候需要 再把 双引号加上

javascript 复制代码
  this.imageUrlList = data.image.split(',')

        this.imageUrlList.unshift('') //落地页回显

目的是未来 "" 数组的第一项数据 是给 上传使用的 里面有判断 k==0 的时候 显示上传按钮 是添加操作

有时候还需要检测 数组里面是不是只要 一个 空字符串

javascript 复制代码
  if (this.imageUrlList.includes('')) {
        } else {
          this.imageUrlList.unshift('') //落地页回显
        }

2、具体代码如下

html 复制代码
   <div
                v-for="(itemd, k) in imageUrlList"
                class="qdsng"
                :key="k"
                style="display: inline-block; position: relative"
              >
                <!-- {{ itemd }} -->
                <a-upload
                  v-if="k == 0"
                  name="avatar"
                  list-type="picture-card"
                  class="avatar-uploader"
                  :show-upload-list="false"
                  :before-upload="beforeUploads"
                  :onRemove="handleRemove"
                  multiple
                  style="padding: 0"
                >
                  <div class="ant-upload-text">上传</div></a-upload
                >
                <a-upload
                  v-else
                  name="avatar"
                  list-type="picture-card"
                  :show-upload-list="false"
                  :before-upload="beforeUploads"
                  :onRemove="handleRemove"
                  id="uploadi"
                >
                  <div v-if="itemd != ''" style="width: 100%; height: 100%; margin: 0 auto">
                    <img
                      v-if="itemd != ''"
                      :src="itemd"
                      alt="avatar"
                      style="width: 100%; height: 100%"
                      @click="uploadd(k)"
                    />
                  </div>
                  <div v-if="itemd == ''">
                    <div class="ant-upload-text" @click="uploadd(k)">上传</div>
                  </div>
                </a-upload>
                <div
                  v-if="itemd != ''"
                  style="
                    position: absolute;
                    top: -2px;
                    right: 3px;
                    z-index: 11111;
                    width: 20px;
                    height: 20px;
                    text-align: center;
                  "
                  @click="handleReset(k)"
                >
                  <a-icon type="close" size="20px" />
                </div>
              </div>
javascript 复制代码
      imageUrlList: [''],//落地页多张图片

      dataindex: '',



  uploadd(data) {
      if (this.uploadshow) {
        this.dataindex = data
      } else {
        this.$message.success('图片上传中,请稍后')
      }
    },


   handleRemove() {
      console.log('取消了')
    },


   handleReset(k) {
      this.imgindex = k
      this.imageUrlList.splice(this.imgindex, 1)

      this.$set(this, 'imageUrlList', this.imageUrlList)
    },

 beforeUploads(file, fileList) {
      console.log(file, fileList, '上传的图片个数')
      this.upimgList = fileList.length
      this.getimng = 1
      let that = this
      this.loading = true
      if (file.type == 'image/png' || file.type == 'image/jpeg') {
        // const isLt10M = file.size / 1024 / 1024 < 2
        // if (!isLt10M) {
        //   return this.$message.error('图片大小不能超过 2MB!')
        // }
      } else {
        return this.$message.error('请上传图片')
      }
      let a = new FormData()
      a.append('file', file)
      a.append('systemId', 1)
      if (this.groupDoctorPhoto) {
        a.append('photoId', this.groupDoctorPhoto.id)
      }
      this.uploadshow = false
      savePhoto(a).then((res) => {
        if (res.code == 200) {
          if (file.type == 'image/png' || file.type == 'image/jpeg') {
            this.fileUrl = res.data
            this.uploadshow = true
            this.loading = false
            // if(Number(this.upimgList) == this.getimng){

            //   this.getimng = 1
            // }else{
            //   this.getimng = this.getimng + 1
            //   //  this.loading = false
            // }
            if (this.dataindex) {
              console.log('修改图片')
              this.imageUrlList[this.dataindex] = res.data
              this.dataindex = ''
            } else {
              console.log('正常添加图片')
              this.imageUrlList.push(res.data)
            }

            // this.$set(this.imageUrlList, this.dataindex, res.data)
            // console.log(this.imageUrlList,'图片列表集合')
            // this.changet('',res)
            // if (this.imgindex) {
            //   // console.log('进入修改')
            //   // this.$nextTick(()=>{
            //   //   this.dataSource[this.dataindex - 1].photoList[this.imgindex] = res.data
            //   // })
            //   this.$set(this.dataSource[this.dataindex - 1][this.dataList], this.imgindex, res.data)
            //   // (this.dataSource[this.dataindex - 1][this.dataList]).push(res.data)
            //   this.imgindex = ''
            // } else {
            //   // console.log(this.dataSource[this.dataindex - 1], this.dataList, '菩萨和')
            //   this.dataSource[this.dataindex - 1][this.dataList].push(res.data)
            // }
          }
        }
      })
      return false
    },




 
相关推荐
用户9385156350713 小时前
手把手教你实现一个 MCP 文件读取服务器:从协议到代码的深度解析
javascript·人工智能
用户21366100357213 小时前
Vue商品详情与放大镜组件
前端·javascript
半个落月13 小时前
从Tapas小Demo理清localStorage、事件与this
前端·javascript
用户9385156350713 小时前
RAG 实战:从零搭建语义搜索系统,彻底告别关键词匹配的尴尬
javascript·人工智能
李明卫杭州13 小时前
Vue2 中 v-model 处理不同数据结构的技巧
前端·javascript·vue.js
李明卫杭州14 小时前
使用 computed 处理 v-model 复杂数据结构
前端·javascript·vue.js
丨我是张先生丨14 小时前
日语单词 Web Page
前端·css·css3
禅思院15 小时前
AI对话前端从入门到崩溃:一个长对话引发的五层优化战争【引子】
前端·面试·架构
TrisighT16 小时前
Electron 鸿蒙 PC 上点外链唤醒应用,我试了 6 种写法只有 1 种能跑
前端·electron·harmonyos
2501_9307077816 小时前
如何将HTML文件转换为纯文本(详细步骤指南)
前端·html