【Andv】Andv图片上传组件:


一、效果图:

在这里插入图片描述

二、实现思路:

把andv的上传组件封装起来,并全局注册,这样别的页面不用引入就可以直接使用

三、实现代码:
【1】components/AndvImageUpload/index.vue
<template>
  <div class="clearfix">
    <a-upload action="#" list-type="picture-card" :before-upload="beforeUpload" multiple :file-list="fileList"
      @preview="handlePreview" @change="handleChange">
      <div v-if="fileList.length < Number(limit)"><a-icon type="plus" />
        <div class="ant-upload-text">请选择图片</div>
      </div>
    </a-upload>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>


<script>
function getBase64(file) {//file转Base64的方法
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}
function getDefaultFileList(Array) {//处理反显图片的方法
  let arr = []
  for (let i = 0; i < Array.length; i++) {
    const item = Array[i];
    if (typeof item == 'object') {
      !item.uid ? item.uid = -i - 1 : null   // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突,这个必需要;否则图片不显示
      !item.name && item.fileName ? item.name = item.fileName : null
      !item.name && item.url ? item.name = item.url.match(/[^/]+(?!.*\/)/g).pop() : null // 正则获取url中的图片名称
      arr.push(item)
    } else {
      arr.push({ uid: -i - 1, name: item.match(/[^/]+(?!.*\/)/g).pop(), url: item })
    }
  }
  return arr;
}

export default {
  props: {
    defaultFileList: {//默认已有的图片反显
      type: [Object, Array],
      default: () => { }
    },
    limit: {//限制图片的数量,最大9张
      type: [String, Number],
      default: 9
    },
  },
  data() {
    return {
      previewVisible: false,
      previewImage: '',
      fileList: [],
    };
  },
  watch: {
    defaultFileList() {
      this.fileList = getDefaultFileList(this.defaultFileList)
    }
  },
  methods: {
    beforeUpload(file) {
      this.fileList = [...this.fileList, file];
      return false;
    },
    handleCancel() {
      this.previewVisible = false;
    },
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      this.previewImage = file.url || file.preview;
      this.previewVisible = true;
    },
    handleChange({ fileList }) {
      this.fileList = fileList;
      this.$emit('allImageList', this.fileList)
    },
  },
};
</script>


<style scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: rgba(0, 0, 0, 0.8);
  font-weight: 500;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: rgba(0, 0, 0, 0.8);
  font-weight: 500;
}
</style>
【2】main.js
// 全局注册图片上传
import AndvImageUpload from '@/components/AndvImageUpload/index.vue'
Vue.component('AndvImageUpload', AndvImageUpload)
【3】使用:

在这里插入图片描述

<AndvImageUpload ref="AndvImageUpload" :defaultFileList="model.fileList" limit='1' @allImageList="getAllImageList" />

在这里插入图片描述

edit(record) {
  this.model = Object.assign({}, record, this.bdidInfo);
  this.model.fileList = ['https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png']
  this.visible = true;
},
// 获取全部的图片
getAllImageList(e) {
  console.log("图片", e)
  // if (!e || e.length == 0) {
  //   this.$message.error("上传图片不能为空!")
  //   return
  // }
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sun Peng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值