Elementui el-upload 图片列表缩略图模式添加查看大图含删除功能

news/2024/7/10 2:35:00 标签: vue, elementui

前言:由于官网el-upload 图片列表缩略图模式没有查看大图的功能,所有自己自定义了一个,其中很多投机取巧的方式,写的不好,若有更好的方法,请留言哦(),不多说废话,直接贴

<template>
<el-upload 
	class="upload-el" 
	accept=".jpg,.png,.bmp" //限制上传的图片类型
	:show-file-list="true" //是否显示已上传文件列表
	multiple //设置多选
	:limit="3" //最大允许上传个数
	:on-exceed="handleExceed" //文件超出个数限制时的钩子
	:file-list="fileArr" //上传的文件列表
	:action="uploadConfig.uploadFileUrl" //必选参数,上传的地址
	:headers="uploadConfig.headers" //设置上传的请求头部例如一些token信息
	:on-error="uploadError" //文件上传失败时的钩子
	:on-success="uploadSuccess" //文件上传成功时的钩子
	list-type="picture">
	<el-button size="small" class="uplaodBtn">上传</el-button>
	<template #tip>
		<div class="el-upload__tip">请上传格式为jpg、png、bmp的图片,且确保图片内容清晰可见</div>
		   <!-- 关键代码在这里,这段代码是我将list-type="picture"的时候显示出来的缩略图的代码复制出来修改的 这里无法覆盖原来的picture,所有我在ul里面加了一个show的类名,然后在样式里面将原来的隐藏了,机智吧 (*^▽^*) -->
			<ul class="el-upload-list el-upload-list--picture show">
				<li v-for="(item,index) in fileArr"  class="el-upload-list__item is-success" tabindex="0">
					<!--scanBigImg:查看大图的方法  -->
					<img style="cursor: pointer;"  
					     @click="scanBigImg(item)"  
					     class="el-upload-list__item-thumbnail"
						 :src="item.url">
					<a class="el-upload-list__item-name">
					<i class="el-icon-document"></i>
					{{item.name}}
					</a>
					<label class="el-upload-list__item-status-label">
						<i class="el-icon-upload-success el-icon-check"></i>
					</label>
					<!-- onRemove:删除已上传的图片文件 -->
					<i class="el-icon-close" @click="onRemove(item,fileArr)"></i>
				</li>
			</ul>
	</template>
</el-upload>
<!-- 显示大图的dialog  -->
<el-dialog v-model="dialogVisible" style="height: 500px; 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>
<script>
import {ref,reactive} from "vue";
import {ElMessage} from "element-plus";
export default {
	setup() {
	   //这个query不重要哈,这里是我后面需求一次性提交给后台需要用到的
		const query = reactive({
				name: "",
				fileUrlArr: []
			});
		const dialogImageUrl = ref(''),//大图的url
			  dialogVisible = ref(false);
		let fileArr = ref([]);//存放上传成功的文件信息
			const uploadConfig = reactive({
				uploadFileUrl: 'api/uploadFile',//文件上传接口路径
				headers: {
					'token':'这是设置的token信息,根据自己需求设置'
				}
			})

			const uploadError = (res, file) => {
				//上传失败做的操作
				ElMessage.error(res.message);
			}
			const uploadSuccess = (res, file,fileList) => {
			    //上传成功做的操作
				fileArr.value = fileList;//这里将fileList赋值给fileArr,主要是为了后面自定义的缩略图展示那里遍历展示
				query.fileUrlArr.push(res.data);//不重要
			}
			//删除
			const onRemove = (file,fileList) => {
				let arr = fileList,newArr = [];
				for(let i=0;i<arr.length;i++){
					if(file != arr[i]){
						newArr.push(arr[i])
					}
				}
				fileArr.value = newArr;
				query.filePathList.splice(file.response.data, 1)
			}
			//查看大图
			const scanBigImg = (file) => {
				dialogImageUrl.value = file.url;
				dialogVisible.value = true;
			}
			//限制上传个数警告
			const handleExceed = (files, fileList) => {
				ElMessage.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
			}
	}
}
</script>
<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>

好啦,这就大功告成拉!展示一下效果吧,请看下图

查看大图
这是大图的模态框

鼠标上移即可删除图片
在这里插入图片描述


http://www.niftyadmin.cn/n/689720.html

相关文章

TYVJ P1045 洛谷 1388 最大的算式 Label:dp

描述 题目很简单&#xff0c;给出N个数字&#xff0c;不改变它们的相对位置&#xff0c;在中间加入K个乘号和N-K-1个加号&#xff0c;&#xff08;括号随便加&#xff09;使最终结果尽量大。因为乘号和加号一共就是N-1个了&#xff0c;所以恰好每两个相邻数字之间都有一个符号。…

Li的前期工作Level_Set_Evolution_Without_Re-initialization_A_New_Variational_Formulation

注意&#xff1a;因为页面显示原因。里头的公式没能做到完美显示&#xff0c;有须要的朋友请到我的资源中下载 无需进行又一次初始化的水平集演化&#xff1a;一个新的变分公式 Chunming Li , Chenyang Xu , Changfeng Gui , and Martin D. Fox 1.Department of Electrical an…

设置input框只能输入数字并保留两位小数

<el-input oninput"valuevalue.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,$1)"></el-input>

linux基础学习第十二天(shell函数、数组以及yum使用及编译安装)

2016-08-18授课内容&#xff1a;shell编程基础&#xff08;select 循环与菜单、函数&#xff09;yum管理、定制yum仓库 select 循环与菜单select variable in listdo循环体命令done 1、select 循环主要用于创建菜单&#xff0c;按数字顺序排列的菜单项将显示在标准错误上&#…

ElementUi el-table 美化暂无数据状态

前言&#xff1a;当表格数据过长的时候&#xff0c;会出现横向滚动条&#xff0c;这个时候暂无数据就需要拉动滚动条到中间才能看到&#xff0c;导致页面及其不美观&#xff0c;所有&#xff0c;请看下方 正常的空值状态如下 经过修改后 解决方法如下 <template><el…

乱码的解决

可能出现乱码的情况 1.从他人传过来的文件本地打开的就是乱码 解决&#xff1a;确认编码统一 2.传过来的编码本地打开正常&#xff0c;eclipse中为乱码 解决&#xff1a;先设置eclipse项目的编码&#xff0c;再倒入文件&#xff0c;必要情况下可以复制粘贴 3.要改变传过来的文件…

[bzoj2502]清理雪道[上下界网络流]

bzoj状态里有两种&#xff0c;一种时间是个位数&#xff0c;一种是四位数&#xff0c;我就是四位数的那种&#xff0c;&#xff0c;&#xff0c;估计都是看了hzwer.. 1 #include <bits/stdc.h>2 3 #define INF 0x3f3f3f3f4 5 using namespace std;6 7 template<…

瀑布流的实现、动态数据绑定、简单封装可根据需求动态传入一行几列的布局

静态实现 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>瀑布式布局</title> </head> <style> .falls_flex_box{display: flex;flex-direction: row;width: 500px;margin: auto; …