【el-upload】el-upload组件 - list-type=“picture“ 时,文件预览展示优化

目录

问题图

el-upload

html 复制代码
<el-upload
  ref="upload"
  multiple
  drag
  action="#"
  :auto-upload="false"
  :file-list="fileList"
  name="files"
  :accept="`.png,.jpg,.jpeg,.JGP,.JPEG,.PNG,.doc,.docx,.xls,.xlsx${isTz ? ',.mp4,.MP4' : ''}`"
  :on-change="beforeAvatarUpload"
  list-type="picture"
>
  <i class="el-icon-upload" />
  <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  <div slot="tip" class="el-upload__tip">
    支持上传png/jpg/jpeg/doc/docx/xls/xlsx文件,且不超过5M
  </div>
  <!-- 【使用组件】 -->
  <div slot="file" slot-scope="{ file }">
    <PicturePreview :file="file" @remove="removeFile" />
  </div>
</el-upload>

import PicturePreview from '@/components/PicturePreview'

components:{PicturePreview}

removeFile(file) {
  this.$refs.upload.handleRemove(file)
},

预览组件 PicturePreview

html 复制代码
<!-- el-upload组件 - list-type="picture" 时,文件预览展示优化 -->
<template>
  <div class="file_container">
    <div class="file">
      <el-image
        v-if="/(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)$/.test(fileType)"
        :src="file.url"
        :preview-src-list="[file.url]"
      />
      <i v-else class="el-icon-tickets" />
    </div>
    <div class="name">{{ file.name }}</div>
    <i class="el-icon-close" @click="$emit('remove', file)" />
  </div>
</template>

<script>
export default {
  name: 'PicturePreview',
  components: {},

  props: {
    file: {
      type: Object,
      required: true
    }
  },

  data() {
    return {}
  },

  computed: {
    fileType() {
      return this.file.name.split('.').pop()
    }
  },

  watch: {},

  created() {},

  methods: {}
}
</script>

<style lang='scss' scoped>
.file_container {
  position: relative;
  .file {
    $size: 80px;
    width: $size;
    height: $size;
    background-color: #f7f7f7;
    position: absolute;
    left: -85px;
    top: -5px;

    display: flex;
    align-items: center;
    justify-content: center;
    .el-image {
      width: 100%;
      height: 100%;
    }
    .el-icon-tickets {
      font-size: 30px;
    }
  }
  .name {
  }
  .el-icon-close {
    position: absolute;
    top: 5px;
    right: 5px;
    font-size: 10px;
  }
}
</style>

效果展示

相关推荐
xuehuayu.cn41 分钟前
js es6 class 类中的值是异步赋值, 子类中如何获取这个值?
javascript·es6
威风的虫1 小时前
ES6 数组方法:告别循环,拥抱函数式编程
开发语言·前端·javascript
小杨快跑~1 小时前
ES6 Promise:告别回调地狱的异步编程革命
前端·javascript·ecmascript·es6
r0ad1 小时前
读诗的时候我却使用了自己研发的Chrome元素截图插件
前端·javascript·chrome
知识分享小能手2 小时前
jQuery 入门学习教程,从入门到精通, jQuery在HTML5中的应用(16)
前端·javascript·学习·ui·jquery·html5·1024程序员节
七号练习生.c3 小时前
JavaScript基础入门
开发语言·javascript·ecmascript
一 乐3 小时前
高校教务|教务管理|基于springboot+vue的高校教务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·教务管理
zhousenshan3 小时前
ES6 import语法
vue.js
码界筑梦坊3 小时前
243-基于Django与VUE的笔记本电脑数据可视化分析系统
vue.js·python·信息可视化·数据分析·django·毕业设计·echarts
baozj3 小时前
🚀 手动改 500 个文件?不存在的!我用 AST 撸了个 Vue 国际化神器
前端·javascript·vue.js