element 实现头像上传和删除

 封装了组件,通过v-model实现,图片展示和删除

data(){
    return {
        formData:{
                iocUrl:''
        }    
    }
} 
 
<el-form-item label="机构图标" prop="iocUrl">
          <UploadHeader v-model="formData.iocUrl" />
        </el-form-item>

图片组件 

<template>
  <el-upload :headers="headers" action="/api/oss/file/upload" :disabled="disabled" :show-file-list="false" accept="image/jpeg,image/png" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
    <img v-if="value" :src="value" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    <i class="el-icon-error" @click.stop="delImg()" v-if="value && !disabled"></i>
  </el-upload>
</template>

<script>
import { Loading } from 'element-ui'
let loadingInstance = null
export default {
  data () {
    return {
      headers: {
        Authorization: 'Bearer ' + localStorage.getItem('Authorization')
      }
    }
  },
  props: {
    value: {
      default: ''
    },
    disabled: {
      default: false
    }
  },
  model: {
    prop: 'value',
    event: 'getValue'
  },
  methods: {
    getValue (item) {
      this.$emit('getValue', item)
    },
    beforeAvatarUpload (file) {
      loadingInstance = Loading.service(this.$store.state.LoadingoOptions)
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
        loadingInstance.close()
      }
      return isLt2M
    },
    handleAvatarSuccess (res, file) {
      if (res.code !== 200) {
        this.$message.error(res.msg)
        return false
      }
      this.getValue(res.data.filePath)
      loadingInstance.close()
      this.$message.success('上传成功')
    },
    delImg () {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.getValue('')
      }).catch(() => { })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-upload {
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .el-upload:hover {
  border-color: #409eff;
}
/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  line-height: 60px;
  text-align: center;
}
/deep/ .avatar {
  width: 60px;
  height: 60px;
  display: block;
}
.el-icon-error {
  position: absolute;
  right: 3px;
  top: 3px;
  z-index: 9;
  font-size: 16px;
  cursor: pointer;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 0 2px #333;
}
</style>

loading配置放在vuex中

  LoadingoOptions: { // loading配置
      lock: true,
      spinner: 'el-icon-loading',
      customClass: 'win-loading'
    },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学不会•

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

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

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

打赏作者

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

抵扣说明:

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

余额充值