定制Element-UI 图片上传组件

业务中有需要使用上传图片的场景,于是打算写一个通用的组件。
上传时会带额外的参数path字段,用于告诉后端是哪个模块下,比如logo、头像等。
话不多说,直接撸代码吧

<template>
  <div>
    <el-upload
      class="upload-demo"
      action="1122"
      list-type="picture-card"
      :before-upload="beforeUpload"
      :on-success="onUploadSuccess"
      :on-preview="handlePictureCardPreview"
      :file-list="fileList"
      :http-request="doUpload"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="file" slot-scope="{file}">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
            <i class="el-icon-zoom-in"/>
          </span>
          <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
            <i class="el-icon-delete"/>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="imageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import { Component, Vue } from 'vue-property-decorator'
  import { uploadImage } from '@/api/common'
  import { removeArrEle } from '@/utils'

  @Component({
    props: {
      path: {
        type: String,
        default() {
          return 'common'
        }
      },
      initImage: String
    },
    watch: {
      initImage: function(newValue, oldValue) {
        this.doWatchInitImage(newValue)
      }
    }
  })
  export default class UploadImagePreview extends Vue {
    dialogVisible = false
    disabled = false
    fileList = []
    imageUrl = null

    created() {
      // this.refresh(this.initImage)
    }

    refresh(url) {
      this.fileList = []
      if (url) {
        this.fileList.push({ url: url })
      }
    }

    doWatchInitImage(newVal) {
      if (!newVal && this.fileList.length > 0) {
        this.fileList = []
      } else if (newVal && this.fileList.length === 0) {
        this.fileList.push({ url: newVal })
      } else {
        this.refresh(newVal)
      }
    }

    beforeUpload(file) {
      return true
    }

    onUploadSuccess(res, file) {
    }

    doUpload(params) {
      const data = { path: this.path, file: params.file }
      uploadImage(data).then(response => {
        this.fileList = []
        this.fileList.push(params.file)
        this.fileList[0].url = response.data
        this.$emit('on-image-change', { url: response.data, desc: 'upload done' })
      }).catch(err => {
        console.log(err.msg)
        removeArrEle(this.fileList, params.file)
      })
    }

    handleRemove(file) {
      removeArrEle(this.fileList, file)
      if (this.fileList.length === 0) {
        this.$emit('on-image-change', { url: this.initImage, desc: 'remove all' })
      } else {
        this.$emit('on-image-change', { url: this.fileList[0].url, desc: 'newest' })
      }
    }

    handlePictureCardPreview(file) {
      this.imageUrl = file.url
      this.dialogVisible = true
    }

    mounted() {
      this.refresh(this.initImage)
    }
  }
</script>

<style>
</style>

使用时引入该组件后直接使用

<upload-image-preview path="logo" :init-image="initImageUrl" @on-image-change="onImageChange"/>

onImageChange(data) {
     console('新上传图片得到的url =>', data.url)
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值