el-upload on-preview 扩大预览事件点击范围

1.首先说明开发环境,vue2项目,采用列表模式的el-upload组件时,加入附件预览功能

element官网给出的示范代码是以下写法

javascript 复制代码
<el-upload
  class="upload-demo"
  action="https://jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :file-list="fileList"
  list-type="picture">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<script>
  export default {
    data() {
      return {
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      }
    }
  }
</script>

此时遇到的问题是,点击名字可以触发预览函数,点击缩略图没效果

我们需要改变一下写法,这些在官网里边都有

javascript 复制代码
<el-upload>
        <el-button size="small" type="primary">点击上传附件</el-button>
        <div class="el-upload__tip" slot="tip">提示信息</div>
        
        <template slot="file" slot-scope="{file}">
            <img @click="handlePreview" :src="file.url" class="el-upload-list__item-thumbnail">
            <p @click="handlePreview" class="el-upload-list__item-name">{{ file.name }}</p>
            <i @click="handleRemove" class="el-icon-close"></i>
        </template>
    </el-upload>

这样就可以把预览事件同时加在名字和缩略图上,进而配合预览组件使用即可

相关推荐
Rsun0455139 分钟前
React相关面试题
前端·react.js·前端框架
Lao乾妈官方认证唯一女友:D1 小时前
通过plasmo的wallet扩展添加新钱包
javascript·web3·区块链
ALKAOUA1 小时前
力扣面试150题刷题分享
javascript·笔记
鹏多多.1 小时前
Flutter使用screenshot进行截屏和截长图以及分享保存的全流程指南
android·前端·flutter·ios·前端框架
LawrenceLan1 小时前
37.Flutter 零基础入门(三十七):SnackBar 与提示信息 —— 页面反馈与用户交互必学
开发语言·前端·flutter·dart
怪侠_岭南一只猿1 小时前
爬虫工程师入门阶段一:基础知识点完全学习文档
css·爬虫·python·学习·html
迪巴拉15251 小时前
基于Vue与Spring Boot+Open Cv的智慧校园考勤系统
前端·vue.js·spring boot
swipe2 小时前
JavaScript 对象与属性描述符:从原理到实战
前端·javascript·面试