Element UI 上传图片组件(支持多传和单传),报错Cannot set property 'status' of null

使用组件页面代码:

<d2-upload v-model="form.uploaderUrlArray" :limit="5" accept=".png,.jpg,.jpeg" multiple />

组件代码,我这里图片是上传到七牛服务器的。注意fileLIst是只读的,不能修改。我们这里使用uploadList来保存我们需要改动的数组,否则报错Cannot set property 'status' of null

<template>
  <div :class="{exceed: isExceed}">
    <el-upload action="https://up.qbox.me/" :data="postUploadData" :before-upload="beforeUpload"
      list-type="picture-card" :on-preview="handlePictureCardPreview" :file-list="fileList" :on-remove="handleRemove"
      v-bind="$attrs" :on-exceed="handleOnExceed" :on-success="handleOnSuccess">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <image-viewer :z-index="3000" v-if="showViewer" :on-close="closeViewer" :url-list="previewSrcList" />
  </div>
</template>
<script>
import imageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  name: 'd2-upload',
  components: { imageViewer },
  props: {
    value: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      options: [],
      fileList: [],
      uploadList: [], // 自定义的数组,用于处理fileList,fileList是只读的
      dialogImageUrl: '',
      fileUrl: '',
      postUploadData: {},
      dialogVisible: false,
      isInternal: false,
      isExceed: false,
      showViewer: false,
      previewSrcList: []
    }
  },
  watch: {
    value: {
      handler (val) {
        if (!this.isInternal) {
          if (!this.uploadList.length) {
            this.fileList = val.map(item => {
              return {
                url: item
              }
            })
          }
          this.setIsExceed()
        } else {
          this.isInternal = false
        }
      },
      immediate: true
    }
  },
  methods: {
    async beforeUpload (file) {
      let res = await this.getUploadToken({
        mimeType: file.type || file.raw.type,
        type: 'IMAGE'
      })
      this.postUploadData = {
        token: res.token,
        key: res.fileName
      }
      this.uploadList.push(res.fileUrl)
      return true
    },
    getUploadToken (data = {}) {
      return new Promise((resolve, reject) => {
        this.$http.get('/cl-system/medias/uploadToken', data).then(res => {
          resolve(res.data)
        }).catch((res) => {
          reject(res)
        })
      })
    },
    handleOnExceed (file, fileList) {
      this.$message.warning('数量超出限制')
    },
    handleRemove (file, fileList) {
      let index = this.fileList.findIndex(item => {
        return file.url === item.url
      })
      this.uploadList.splice(index, 1)
      this.isInternal = true
      this.fileList = fileList
      this.emitData(this.uploadList)
      this.isExceed = false
    },
    handlePictureCardPreview (file) {
      this.previewSrcList = this.fileList.map(item => item.url)
      this.showViewer = true
    },
    handleOnSuccess (response, file, fileList) {
      this.fileList = fileList
      this.isInternal = true
      this.setIsExceed()
      this.emitData(this.uploadList)
    },
    emitData (list) {
      this.$emit('input', list)
    },
    closeViewer () {
      this.showViewer = false
    },
    setIsExceed () {
      if (this.$attrs.limit && this.fileList.length >= this.$attrs.limit) {
        this.isExceed = true
      } else {
        this.isExceed = false
      }
    }
  },
  mounted () {
  }
}
</script>
<style scoped>
.exceed >>> .el-upload {
  display: none;
}
</style>

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值