vue3 ElementPlus el-upload 图片列表缩略图模式添加查看大图 删除等自定义按钮

列表缩略图自定义按钮如图:

在这里插入图片描述

在这里插入图片描述
代码:
html部分

<template>
  <div class="container">
    <!-- :show-file-list="false" -->
    <el-upload v-model:file-list="state.list" class="picture-card" action="" list-type="picture" 
      :http-request="handleUpload" :on-change="handleChange" limit="3">
      <el-button type="primary">点击上传图片</el-button>
      <template #tip>
        <div class="el-upload__tip">请上传图片,且确保图片内容清晰可见</div>
        <!-- 关键代码在这里,这段代码是将list-type="picture"的时候显示出来的缩略图的代码复制出来修改的 这里无法覆盖原来的picture,所有    在ul里面加了一个show的类名,然后在样式里面将原来的隐藏了-->
        <ul class="el-upload-list el-upload-list--picture show">
          <li v-for="(item, index) in state.list" class="el-upload-list__item is-success" tabindex="0">
            <!--scanBigImg:查看大图的方法  -->
            <div style="width:100%;display: flex;justify-content: space-between;">
              <div>
                <img style="cursor: pointer;" @click="scanBigImg(item)" class="el-upload-list__item-thumbnail"
                  :src="item.url">
              </div>
              <div style="margin-right:30px;margin-top:20px;">
                隐藏 <el-switch @change="handleSwitch(item, index)" v-model="item.isHide" />
                <el-icon style="font-size: 18px;margin-left:20px;padding-top:10px" @click="handleDelete(index)">
                  <Delete />
                </el-icon>
              </div>
            </div>
            <label class="el-upload-list__item-status-label">
              <el-icon>
                <Check />
              </el-icon>
            </label>
          </li>
        </ul>
      </template>
    </el-upload>
    <el-button type="primary" @click="submit">确定保存</el-button>
  </div>
  <!-- 显示大图的dialog  -->
  <el-dialog v-model="dialogVisible" style="max-height: 1000px; width: 100%; text-align: center;">
    <img style="width: auto; max-width: 100%; height: auto; max-height: 500px; display: inline-block;margin: auto;"
      :src="dialogImageUrl" alt="">
  </el-dialog>
</template>

js

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import type { UploadProps, UploadUserFile } from 'element-plus'
import { Delete, Check } from '@element-plus/icons-vue'

const dialogImageUrl = ref('')//大图的url
const dialogVisible = ref(false);
//查看大图
const scanBigImg = (file) => {
  dialogImageUrl.value = file.url;
  dialogVisible.value = true;
}
//删除
const handleDelete = (index: number) => {
  state.dataList.splice(index, 1)
  state.list.splice(index, 1)
}



</script>

css:

<style scoped>
:deep(.el-upload-list.el-upload-list--picture) {
  display: none !important; //这里设置原有的缩略图视图隐藏
}

.el-upload-list.el-upload-list--picture.show {
  display: block !important; //展示自定义
}
</style>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3中,可以使用el-upload组件上传大小超过60M的大幅图片。但是需要注意的是,默认情况下,el-upload组件有一个最大上传文件大小的限制,这个限制可以在组件中进行配置。 以下是一个上传大小超过60M的大幅图片el-upload组件的示例代码: ```html <template> <el-upload class="upload-demo" action="/upload" :on-exceed="handleExceed" :before-upload="beforeUpload" :limit-size="100000000" :file-list="fileList" multiple> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传不超过 100MB 的文件</div> </el-upload> </template> <script> export default { data() { return { fileList: [] } }, methods: { handleExceed(files, fileList) { this.$message.warning(`文件数量超过限制,只能上传一个文件`); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg'; const isLt100M = file.size / 1024 / 1024 < 100; if (!isJPG) { this.$message.error('上传图片只能是 JPG 格式!'); } if (!isLt100M) { this.$message.error('上传图片大小不能超过 100MB!'); } return isJPG && isLt100M; } } } </script> ``` 在上面的代码中,我们通过在el-upload组件中设置limit-size属性来设置最大上传文件大小。同时,我们在beforeUpload方法中对上传的文件进行了格式和大小的判断,如果不符合要求则会弹出提示框。如果需要上传的文件数量超过限制,则会触发handleExceed方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值