H5 图片上传组件之使用

391 阅读1分钟

小知识,大挑战!本文正在参与「程序员必备小知识」创作活动

上一篇H5 图片上传组件对图片上传组件进行了封装,完成了权限问题、图片上传、图片压缩、图片 转 base64 格式等

这一篇文章封装了一个通用的图片预览组件;介绍组件的使用,可以进行上传、预览、删除等操作。

图片预览组件

  1. base64 格式:对于新增的图片,图片上传组件中已经将图片转为 base64 格式,可以通过 img 的 src 预览,直接赋值:src="picUrl"
  2. 链接形式:对于编辑时,已经存在的图片,通过链接形式预览,:src="//${ picUrl }"
<template>
  <div v-if="visible" class="preview-wrap mask">
    <div class="preview-icon">
      <img v-if="viewType==='base64'" :src="picUrl" class="icon" />
      <img v-if="viewType==='backUrl'" :src="`//${picUrl }`" class="icon" />
    </div>
    <div @click="onCancel" class="common-submit btn-close">关闭</div>
  </div>
</template>

<script>
  export default {
    name: "Preview",
    props: {
      visible: {
        type: Boolean,
        default: false,
      },
      picUrl: {
        type: String,
        default: "",
      },
      viewType: {
        type: String,
        default: "base64",
      },
    },
    methods: {
      onCancel() {
        this.$emit("on-cancel");
      },
    },
  };
</script>

<style lang="scss" scoped>
  .preview-wrap {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 500;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    &.mask {
      bottom: 0;
      height: 100%;
      background-color: $colorLightBlack;
    }

    .preview-icon {
      margin-bottom: 0.4rem;

      .icon {
        width: 6.7rem;
        max-height: 9rem;
        border-radius: 0.12rem;
      }
    }
    .btn-close {
      margin: 0 auto;
      width: 3.2rem;
      background-color: $bgWhiteColor;
      color: $color000;
    }
  }
</style>

整体组件使用

<template>
  <div class="feedback-wrap">
    <div class="f-upload-list">
      <div v-for="(item, key) in picList" :key="key" class="upload-item">
        <img :src="item.uri" @click="previewImg(item)" class="item-img" />
        <img
          @click.stop="delImg(item)"
          src="/images/icon_close.png"
          class="item-del"
        />
      </div>
      <upload-image-component
        v-if="picList.length < 3"
        @on-upload="uploadImg"
      />
    </div>
    <preview-component
      :visible="showPreviewImg"
      :pic-url="picUrl"
      @on-cancel="showPreviewImg=false"
    />
  </div>
</template>

<script>
  import PreviewComponent from "~/components/common/Preview";
  import UploadImageComponent from "~/components/common/UploadImage";
  export default {
    components: {
      PreviewComponent,
      UploadImageComponent,
    },
    data() {
      return {
        picList: [],
        picUrl: "",
        showPreviewImg: false,
      };
    },
    methods: {
      // 图片上传
      uploadImg(item) {
        const picList = this.picList.slice();
        picList.push(item);
        this.picList = picList;
      },
      // 删除图片
      delImg(item) {
        const picList = this.picList.slice();
        picList.map((value, index) => {
          if (value.time === item.time) {
            picList.splice(index, 1);
          }
        });
        this.picList = picList;
      },
      // 图片预览
      previewImg(item) {
        this.picUrl = item.uri;
        this.showPreviewImg = true;
      },
    },
  };
</script>

<style lang="scss" scoped></style>