element用户上传头像组件带大图预览,和删除功能

element 用户上传组件不支持大图预览,拿组件的简单修改一些,发表上来主要是记一下,以后可以用

效果图

<template>
    <div class="flex-img">
      <div class="el-upload-list el-upload-list--picture-card" v-show="hideShow">
        <div class="el-upload-list__item is-success">
          <img class="flex-img__image" :src="imageUrl">
          <label class="el-upload-list__item-status-label">
            <i class="el-icon-upload-success el-icon-check"></i>
          </label>
          <span class="el-upload-list__item-actions" >
                          <span
                            @click="handlePictureCardPreview()"
                          >
                      <i class="el-icon-zoom-in"></i>
                    </span>
                    <span class="el-upload-list__item-delete" v-show="disabledBoolean ? false:true">
                        <i class="el-icon-delete" @click.stop="handleRemove()"></i>
                    </span>
                </span>
        </div>
      </div>
      <el-upload
        class="image-uploader"
        :show-file-list="false"
        :headers="headers"
         action="/api/blade-resource/oss/endpoint/put-file"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeUpload"
        v-show="!hideShow">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible"     append-to-body>
        <img width="100%" :src="imageUrl" alt="">
      </el-dialog>
  </div>
</template>

<script>

import {getToken} from "@/util/auth";

export default {
  name: "uploadImage.vue",
  data() {
    return {
      file: this.imageUrl ? this.imageUrl : '',
      headers:"", //上传地址
      imageForm:"",//给父组件传值
      dialogVisible: false,//控制大图预览
    };
  },
  props:{
    imageUrl:{//父组件传值过来照片回显
      type:String,
      dispatch() {
        return "";
      }
    },
    disabledBoolean:{//父组件传过来是编辑,还是查看
      type: Boolean,
      default() {
        return false;
      }
    },
  },
  watch:{
    imageUrl(value) {
      this.file = value
    },
    imageForm(value) {//当照片地址改变的时候,给父组件传值
      const list = value;
      this.$emit('updateImage', list);
    }
  },
  computed: {
    hideShow() {//当图片多于一张的时候,就隐藏上传框
      return this.file === '' ? false : true
    }
  },
  methods: {

    handlePictureCardPreview() {
      this.dialogVisible = true;
    },
    beforeUpload(file) {
      const imageSize = file.size / 1024 / 1024 < 1;//上传图片大小不超过1M
      if (!imageSize) {
        this.$message.error('上传封面大小不能超过 1MB!');
      }
      return imageSize;
    },
    //删除照片是清空所有
    handleRemove() {
      this.file = '';
      this.imageUrl="";
      this.imageForm="";
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl =  URL.createObjectURL(file.raw);
      this.imageForm = res.data.name;
    },

  }
}
</script>

<style scoped>
.flex-img {
  display: flex;
}
.image-uploader {
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
  border-radius: 6px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 148px;
  height: auto;
  cursor: pointer;
  line-height: 146px;
  vertical-align: top;
  text-align: center
}

.image-uploader {
  font-size: 28px;
  color: #8c939d;
}

.image-uploader .el-upload:hover {
  border-color: #409EFF;
}

.flex-img__image {
  width: 146px;
  height: auto;

  border-radius: 6px;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值