uploadImg.vue 1.47 KB
<template>
  <div>
    <el-upload
      action="http://192.168.110.67/mall/file/image/upload"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :headers="headers"
      :file-list="fileList"
      accept=""
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [
        {
          name: "",
          url: "",
        },
      ],
    };
  },
  computed: {
    headers() {
      return {
        Authorization: localStorage.getItem("backToken") || "",
      };
    },
  },
  methods: {
    // 图片上传成功的返回值
    handleAvatarSuccess(res, file) {
      if (res.resultCode == 200) {
        this.$emit("imgUrl", res.data.url);
        this.imageUrl = res.data.url;
      } else {
        this.$message.error(res.message);
      }
    },
    handleRemove(file, fileList) {
      this.$emit("imgUrl", "");
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    showImg(cover) {
      let file = {
        name: "",
        url: cover,
      };
      this.fileList = [file]
    },
  },
};
</script>

<style>
.avatar {
  width: 100%;
  height: 100%;
}
</style>