将el-upload触发方式改为双击

5 篇文章 0 订阅
2 篇文章 0 订阅

 将el-upload的触发方式由单击变为双击

原理就是通过下图代码将el-upload设置成disable不可以点击

if(this.testData.photo_path) this.imgDisable = true
//但img标签没有地址时,即无内容时

 然后再通过双击事件@dbclick实现触发

resetImgUpload(){
    this.imgDisable = false;
    this.$refs.upload.$children[0].$refs.input.click();
    this.imgDisable = true;
}

 完整代码如下,该部分代码为子组件代码,自行区分。

<template>
  <div class="imgbody">
    <el-upload class="upload-demo" ref="upload" action="" :before-remove="beforeRemove" :on-change="onUploadChange" :on-exceed="handleExceed" :disabled="imgDisable">
      <img v-if="testData.photo_path" :src="testData.photo_path" class="avatar" @dblclick="resetImgUpload">
      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
/* eslint-disable */

export default {
  name:"imageChart",
  props:{
    testData:{
      default:function(){
        return ""
      }
    },
  },
  data () {
    return { 
      imgUrl:'',
      imgDisable:true
    }
  },
  watch:{
  },
  created() {
    if(this.$route.name == 'AddDashBorad'){
      this.imgDisable = false
    }else{
      this.imgDisable = true
    }
    if(this.testData.photo_path) this.imgDisable = true
  },
  methods:{
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }?`);
    },
    onUploadChange(file,fileList){
      fileList = [];
      const isIMAGE = (file.raw.type === 'image/jpeg' || file.raw.type === 'image/png'|| file.raw.type === 'image/gif');
      if (!isIMAGE) {
        this.$message.error('上传文件只能是图片格式!');
        return false;
      }
      var reader = new FileReader();
      reader.readAsDataURL(file.raw);
      let _this = this
      reader.onload = function(e){
        _this.testData.photo_path = this.result
      }
      //  = this.imageUrl = this.imageUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'|| 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传图片必须是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    resetImgUpload(){
      this.imgDisable = false;
      this.$refs.upload.$children[0].$refs.input.click();
      this.imgDisable = true;
    }
  },
}
</script>
<style>
  .imgbody{
    display: flex;
    overflow: hidden;
    margin-top: 1%;
    width: 100%;
    justify-content: center;
    align-items: center;
  }
  .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: 36px;
    color: #8c939d;
  }
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

攻城狮炭烤策划

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

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

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

打赏作者

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

抵扣说明:

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

余额充值