上传图片组件

<template>
  <div class="upload-image">
    <div v-if="imageList.length > 0 || isEdit" class="image-list">
      <el-upload
        :show-file-list="false"
        :on-change="handleAvatarSuccess"
        :auto-upload="false"
        :multiple="true"
        action
        class="image-item"
      >
        <div class="upload-image">
          <i class="el-icon-plus" />
        </div>
      </el-upload>
      <div v-for="(item, index) in imageList" :key="item.uid" class="image-item" >
        <el-image
          :src="item.url"
          :preview-src-list="imageList.map(value => value.url)"
          class="video"
          fit="contain"
        />
        <div class="del-btn-icon" @click="deleteImage(index)"><i class="iconfont-ziranziyuan iconshanchu1" /></div>
      </div>
    </div>
    <div v-else>无</div>
  </div>
</template>

<script>
export default {
  name: 'UploadImage',
  props: {
    isEdit: {
      type: Boolean,
      default: true
    },
    tpdz: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      imageList: []
    }
  },
  created() {
    this.imageList = [...this.tpdz]
  },
  methods: {
    handleAvatarSuccess(file) {
      const imageType = ['svg', 'jpg', 'png', 'pdf', 'gif', 'jpeg', 'bmp', 'dwg', 'dxf', 'dwt']
      const fileType = file.name.split('.')[1]
      if (!imageType.includes(fileType)) {
        this.$mymess.warning('只支持上传图片')
        return false
      } else if (this.imageList.find(item => item.name === file.name)) {
        this.$mymess.warning(file.name + '文件重复')
        return false
      }
      const imageObj = { ...file }
      imageObj.url = URL.createObjectURL(file.raw)
      this.imageList.push(imageObj)
      this.$emit('changeTPDZ', this.imageList)
    },
    // 删除照片
    deleteImage(index) {
      this.imageList.splice(index, 1)
      this.$emit('changeTPDZ', this.imageList)
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-image {
  width: 100%;
  .image-list {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    .image-item {
      display: inline-block;
      margin-bottom: 12px;
      height: 126px;
      width: calc((100% - 48px)/4);
      margin-right: 16px;
      text-align: center;
      line-height: 124px;
      font-size: 32px;
      color: #C0C4CC;
      background-color: #F2F6FC;
      border-radius: 4px;
      border: 1px solid #DCDFE6;
      position: relative;
      /deep/.el-upload {
        width: 100%;
      }
      &:nth-child(4n) {
        margin-right: 0;
      }
      .video {
        width: 100%;
        height: 100%;
      }
      &:hover {
        .del-btn-icon {
          display: block;
        }
      }
      .play-btn-icon {
        width: 24px;
        height: 24px;
        line-height: 24px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #FFFFFF;
        .iconfont-ziranziyuan {
          font-size: 24px;
        }
      }
      .del-btn-icon {
        display: none;
        cursor: pointer;
        position: absolute;
        width: 100%;
        height: 28px;
        bottom: 0;
        left: 0;
        background-color: rgba(0, 0, 0, 0.6);
        font-size: 16px;
        line-height: 28px;
        color: #FFFFFF;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值