【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>

效果展示

相关推荐
bo52100几秒前
从0到1:Element Plus虚拟树的拖拽功能二次开发实战
javascript·vue.js
小约翰仓鼠几秒前
vue3表格使用Switch 开关
前端·javascript·vue.js
满分观察网友z1 分钟前
encodeURIComponent和decodeURIComponent
javascript
程序员小刘4 分钟前
如何优化React Native应用以适配HarmonyOS5?
javascript·react native·react.js·华为·harmonyos
要加油哦~1 小时前
vue · 插槽 | $slots:访问所有命名插槽内容 | 插槽的使用:子组件和父组件如何书写?
java·前端·javascript
前端Hardy2 小时前
HTML&CSS:3D图片切换效果
前端·javascript
spionbo2 小时前
Vue 表情包输入组件实现代码及完整开发流程解析
前端·javascript·面试
全宝2 小时前
✏️Canvas实现环形文字
前端·javascript·canvas
金金金__2 小时前
Element-Plus:popconfirm与tooltip一起使用不生效?
前端·vue.js·element
前端梭哈攻城狮2 小时前
uniapp图片上传添加水印/压缩/剪裁
前端·javascript·vue.js