1.效果(解决循环组件赋值问题)在这里插入图片描述
废话不多说直接上代码
2.下载七牛云依赖

npm install qiniu-js
# 或者使用 yarn
yarn add qiniu-js

3.在vue组件中引入

import * as qiniu from 'qiniu-js'

4.在components文件夹下创建UploadImg1/uploadImg.vue组件

<template><div:style="{display: 'grid','justify-content': 'space-between','grid-template-columns': `repeat(auto-fill,minmax(${w},1fr))`}"class="imgBoxSty"><divclass="img-list-item common mb_10":style="{ width: w }"v-for="(item, index) in fileList":key="index"><!-- <videov-if="!matchType(item.name)":style="{ width: w, height: h, margin: '0 9px' }"controls="controls":src="item.url">您的浏览器不支持视频播放</video> --><el-image@mouseover="srcList = [item]":preview-src-list="srcList":style="{ width: w, height: h, margin: '0 9px' }":src="item"fit="cover"></el-image><i class="del-img" @click="forkImage(index)" v-if="isShowImg == true"></i></div><div v-if="maxlength < limit" @click="change"><el-upload:multiple="imgmultiple"action="":data="dataObj":show-file-list="false":auto-upload="true":on-remove="handleRemove":on-success="handleUploadSuccess":before-upload="beforeUpload":on-progress="uploadVideoProcess":http-request="FileForQiNiu"><spanclass="warpss":style="{ width: w, height: h, lineHeight: h }"v-if="isShowImg == true"><iclass="el-icon-plus":style="{color: '#8C939D',fontSize: '18px',fontWeight: 'bold',padding: paddings}"></i></span></el-upload></div></div>
</template>
<script>
import { GetqiniuToken } from "@/api/tools/qiniu";//这个是获取七牛云token接口,由后端提供
import * as qiniu from "qiniu-js";//引入七牛云依赖
// 或者按需导入(推荐)
export default {name: "uploadImg",props: {//是否多选imgmultiple: {type: Boolean,default: false},//是否需要上传图片(false:需要,true:不需要,只能查看图片不能做任何操作)isShowImg: {type: Boolean,default: false},//个数显示limit: {type: Number,default: 5},maxlength: {type: Number},value: Array,//最大上传图片数量maxCount: {type: Number,default: 5},//宽度w: {type: String// default:'100px'},//高度h: {type: String// default:'100px'},paddings: {type: String},// 上传类型,1图片,2视频,3图片或视频uploadType: {type: Number,default: 1}},data: function() {return {flag: true,srcList: [],videoFlag: false,isShow: true,videoUploadPercent: 0,count: 5,videis: false,dataObj: {policy: "",signature: "",key: "",ossaccessKeyId: "",dir: "",host: ""},dialogVisible: false,dialogImageUrl: []};},computed: {fileList() {return this.value;}},mounted() {if (this.fileList.length < this.limit) {this.isShow = true;} else {this.isShow = false;}},methods: {//图片视频匹配matchType(name) {// console.log("图片视频匹配");//后缀获取let suffic = "";//获取类型结果let result = "";try {let fileArr = name.split(".");suffic = fileArr[fileArr.length - 1];// console.log('suffic',suffic);} catch (error) {suffic = "";}//图片格式var imgList = ["png", "jpg", "jpeg", "bmp", "gif"];//进行图片匹配result = imgList.some(item => {return item === suffic;});// console.log('结果',result);if (result) {result = "image";// console.log('结果',result);return result;}},//删除视频/图片forkImage(index) {console.log("删除视频/图片", this.fileList, "索引", index);// var data = this.fileList.splice(index, 1);// this.$emit("delFile", this.fileList);// if (this.fileList.length < this.limit) {//   this.isShow = true;// } else {//   this.isShow = false;// }// 创建新数组而不是修改原数组// 创建新数组(不要直接修改props)const newFileList = [...this.value];newFileList.splice(index, 1);// 只使用input事件更新数据this.$emit("input", newFileList);// 更新显示状态this.isShow = newFileList.length < this.limit;},change() {// console.log("change函数", this.fileList);if (this.fileList.length < this.limit) {this.isShow = true;} else {this.isShow = false;}},getUUID() {return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {return (c === "x"? (Math.random() * 16) | 0: "r&0x3" | "0x8").toString(16);});},emitInput(fileList) {// console.log("emitInput韩束", fileList);if (!this.flag) {return false;}let value = [];for (let i = 0; i < fileList.length; i++) {value.push(fileList[i]);}this.$emit("input", value);},handleRemove(file, fileList) {if (this.flag) {// console.log("handleRemove函数", file, fileList);this.emitInput(fileList);}},//上传图片/视频成功后的操作handleUploadSuccess(res, file) {console.log("handleUploadSuccess函数上传图片/视频成功");const url =this.dataObj.host +"/" +this.dataObj.key.replace("${filename}", file.name);const newFileList = [...this.value, url];this.$emit("input", newFileList);if (newFileList.length >= this.limit) {this.$message({message: `最多只能上传${this.limit}个文件`,type: "warning",duration: 1000});}},//进度条uploadVideoProcess(event, file, fileList) {this.videoUploadPercent = Math.floor(event.percent);},beforeUpload(file) {this.flag = true;// 上传限制视频//let vedioArr = [// "video/mp4",//  "video/m4v",//  "video/ogg",//  "video/flv",//"video/avi",// "video/wmv",//  "video/rmvb"//  ];// 上传限制图片let imgArr = ["image/jpeg", "image/PNG", "image/gif", "image/png"];// 既可上传图片、视频let bothArr = ["video/mp4","video/m4v","video/ogg","video/flv","video/avi","video/wmv","video/rmvb","image/jpeg","image/PNG","image/gif","image/png"];//视频/图片校验if ([// "video/mp4",// "video/m4v",// "video/ogg",// "video/flv",// "video/avi",// "video/wmv",// "video/rmvb","image/jpeg","image/PNG","image/gif","image/png"].indexOf(file.type) == -1) {this.$message.error("请上传正确的视频/图片格式");this.flag = false;// return false;}console.log("canshu", this.flag);return this.flag;},// 获取七牛云tokenFileForQiNiu(param) {GetqiniuToken().then(response => {console.log("七牛云上传", response);const key ="七牛云创建的文件夹名称/" +this.formatTimestamp(new Date().getTime()) +"_" +param.file.size +param.file.name;const observable = qiniu.upload(param.file,key,response,{},{ useCdnDomain: true });observable.subscribe(res => {param.onProgress({ percent: res.total.percent });},err => console.error(err),res => {const newFileList = [...this.value,"https://域名/" + res.key];this.$emit("input", newFileList);});});},formatTimestamp(timestamp) {// 如果 timestamp 是字符串,尝试转换为数字const time = new Date(typeof timestamp === "string" ? parseInt(timestamp) : timestamp);// 获取各个时间部分const year = time.getFullYear();const month = String(time.getMonth() + 1).padStart(2, "0"); // 月份从0开始,所以要+1const day = String(time.getDate()).padStart(2, "0");const hours = String(time.getHours()).padStart(2, "0");const minutes = String(time.getMinutes()).padStart(2, "0");const seconds = String(time.getSeconds()).padStart(2, "0");// 拼接成格式化的字符串return `${year}_${month}_${day}${hours}${minutes}${seconds}`;}}
};
</script>
<style lang="scss" scoped>
.warpss {display: inline-block;border: 1px dashed #dee5ed;margin-left: 13px;
}::v-deep.el-upload-list {display: none;
}.el-upload-video {width: 149px;height: 149px;border: 1px dashed #d9d9d9;border-radius: 6px;cursor: pointer;position: relative;overflow: hidden;
}.el-upload-video-i {font-size: 20px;font-weight: bold;padding-top: 43px;color: #8c939d;width: 50px;height: 50px;line-height: 50px;text-align: center;
}//视频
.img-list-item {position: relative;margin: auto;
}.img-list-item i.del-img {width: 20px;height: 20px;display: inline-block;background: rgba(0, 0, 0, 0.6);background-image: url(../../assets/images/close.png);//关闭按钮图标background-size: 18px;background-repeat: no-repeat;background-position: 50%;position: absolute;top: 0;right: -7px;
}
.imgBoxSty {// display: flex;// flex-wrap: wrap;// justify-content: flex-start;width: 100% !important;// border: 1px solid;// grid-gap: 40px 30px;
}
</style>

5.父组件引用

<template>
<div>
<div  v-for="(item, index) in form.resources" :key="index"><upload-img@input="handleImageChange(index, $event)":isShowImg="isShowImg":maxlength="item.imageArr.length":limit="1"w="100px"h="100px"v-model="item.imageArr"></upload-img>
</div></div>
</<template>import uploadImg from "@/components/UploadImg1/uploadImg.vue";export default {components: { uploadImg },data() {return {form:{isShowImg:true,resources:[{img: "",activityName: "",jumpType: "", //类型jumpLink: "", //商品/科普/运营/自我诊疗/外部链接参数path: "", //路径imageArr: [] //拿到的图片}]}}},methods: {handleImageChange(index, newValue) {// this.form.resources[index].imageArr = [newValue];// this.form.resources[index].img = newValue[0];// 使用Vue.set确保响应式更新this.$set(this.form.resources, index, {...this.form.resources[index],imageArr: newValue,img: newValue[0] || "" // 保持同步});this.$forceUpdate();console.log("获取图片数据", index, "图片", newValue, this.form.resources);},}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/91881.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/91881.shtml
英文地址,请注明出处:http://en.pswp.cn/bicheng/91881.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025年6月电子学会青少年软件编程(C语言)等级考试试卷(一级)

答案和更多内容请查看网站&#xff1a;【试卷中心 -----> 电子学会 ----> C/C ----> 一级】 网站链接 青少年软件编程历年真题模拟题实时更新 一、编程题 第 1 题 希望如光 题目描述 在充满挑战的生活中&#xff0c;希望往往是支撑人们穿越黑暗的核心力量。这…

拒绝复杂,AI图表制作简单化

在信息爆炸的时代&#xff0c;数据可视化已成为传递信息的核心手段。无论是职场汇报中的业绩分析&#xff0c;还是学术研究里的实验数据呈现&#xff0c;一张清晰直观的图表往往能胜过千言万语。而 AI 技术的介入&#xff0c;彻底改变了图表制作的传统模式 —— 它不仅让零基础…

easypoi生成多个sheet的动态表头的实现

在使用 EasyPOI 导出 Excel 时&#xff0c;生成多个 Sheet 且每个 Sheet 的表头是动态的&#xff08;即每个 Sheet 的列数和列名可能不同&#xff09;&#xff0c;可以通过如下方式实现&#xff1a;✅ 实现原理简述 使用 Workbook workbook ExcelExportUtil.exportExcel(expor…

移除链表元素+反转链表+链表的中间节点+合并两个有序链表+环形链表约瑟夫问题+分割链表

一、移除链表元素 给你一个链表的头节点 phead 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 (列表中的节点数目在范围 [0, 104] 内) 示例&#xff1a;输入&#xff1a;head [1,2,6,3,4,5,6], val 6 …

vue3+arcgisAPI4示例:轨迹点模拟移动(附源码下载)

demo源码运行环境以及配置运行环境&#xff1a;依赖Node安装环境&#xff0c;需要安装Node。 运行工具&#xff1a;vscode或者其他工具。 配置方式&#xff1a;下载demo源码&#xff0c;vscode打开&#xff0c;然后顺序执行以下命令&#xff1a; &#xff08;1&#xff09;下载…

Design Compiler:Milkyway库的创建与使用

相关阅读 Design Compilerhttps://blog.csdn.net/weixin_45791458/category_12738116.html?spm1001.2014.3001.5482 DC Ultra推出了拓扑模式&#xff0c;在综合时会对标准单元进行粗布局(Coarse Placement)并使用虚拟布线(Virtual Routing)技术计算互联延迟&#xff0c;关于拓…

嵌入式教学的云端革命:高精度仿真如何重塑倒车雷达实验与工程教育——深圳航天科技创新研究院赋能新一代虚实融合实训平台

一、嵌入式教学的困境与破局之道 在传统嵌入式系统教学中&#xff0c;硬件依赖始终是核心痛点。以“倒车雷达实验”为例&#xff0c;学生需操作STM32开发板、超声波传感器、蜂鸣器等硬件&#xff0c;面临设备损耗、接线错误、调试效率低等问题。更关键的是&#xff0c;物理硬件…

flutter-boilerplate-project 学习笔记

项目地址&#xff1a; https://github.com/zubairehman/flutter_boilerplate_project/tree/master 样板包含创建新库或项目所需的最小实现。存储库代码预加载了一些基本组件&#xff0c;例如基本应用程序架构、应用程序主题、常量和创建新项目所需的依赖项。通过使用样板代码…

集成电路学习:什么是CMSIS微控制器软件接口标准

CMSIS,即Cortex Microcontroller Software Interface Standard(Cortex微控制器软件接口标准),是由ARM公司与多家不同的芯片和软件供应商紧密合作定义的一个标准。该标准旨在为基于ARM Cortex处理器的微控制器提供一套与供应商无关的硬件抽象层,从而简化软件的开发、重用,…

由浅入深使用LangGraph创建一个Agent工作流

创建一个简单的工作流&#xff1a;Start ——> 节点1(固定输入输出) ——> Endfrom langchain_core.messages import SystemMessage, HumanMessage, AIMessage from langgraph.graph import StateGraph, START, END from typing_extensions import TypedDict from typing…

PL-0功能拓展及基于VSCode的IDE配置

title: PL/0功能拓展及基于VSCode的IDE配置 date: 2024-08-06 22:46:38 tags: 做过的实验||项目复盘 top: true 概述PL/0语言可以看成PASCAL语言的子集,它的编译程序是由C语言编写的编译解释执行系统。PL/0能充分展示高级语言的最基本成分。拓展了pl0语言的基础功能&#xff08…

【低空经济】大型露天矿区安全生产无人机巡查与管理系统设计

1. 引言 大型露天矿区因其广阔的作业区域和复杂的环境条件&#xff0c;安全生产管理面临着严峻的挑战。随着科技的进步&#xff0c;无人机作为一种现代化的巡查工具&#xff0c;逐渐被应用于矿区的安全生产管理中。无人机具备高效、灵活、成本相对低廉等优点&#xff0c;可以在…

SpringCloud学习第一季-3

目录 11.服务网关-Gateway新一代网关 一、Gateway概述 1、Gateway是什么 1.1 概述 2、 能干嘛 3、微服务架构中网关在哪里 4、为什么选择gateway? 4.1 SpringCloud Gateway具有如下特性 4.2 SpringCloud Gateway 与 Zuul的区别 5、Zuul1.x模型 6、gateway模型 二、…

超越边界:MongoDB 16MB 文档限制的 pragmatic 解决方案

在软件开发中&#xff0c;我们选择的技术栈往往带有一些固有的设计边界。对于 MongoDB 而言&#xff0c;其最著名的边界之一便是 BSON 文档最大 16MB 的大小限制。在大多数场景下&#xff0c;这个限制是绰绰有余的&#xff0c;它鼓励开发者设计更为精简和规范的数据模型。然而&…

深入探讨:PostgreSQL正则表达式中的邮政编码匹配

引言 在处理大量数据时,如何高效地从字符串中提取特定模式的文本,如邮政编码,是一个常见且具有挑战性的任务。本文将通过一个具体实例,探讨在PostgreSQL中使用正则表达式匹配加拿大邮政编码的问题,并提供解决方案。 问题描述 我们希望能够从字符串中提取所有符合加拿大…

集合框架(重点)

第十五天集合框架1.什么是集合 Collections集合Collection&#xff0c;也是一个数据容器&#xff0c;类似于数组&#xff0c;但是和数组是不一样的。集合是一个可变的容器&#xff0c;可以随时向集合中添加元素&#xff0c;也可以随时从集合中删除元素。另外&#xff0c;集合还…

深度学习核心:神经网络-激活函数 - 原理、实现及在医学影像领域的应用

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家、CSDN平台优质创作者&#xff0c;高级开发工程师&#xff0c;数学专业&#xff0c;10年以上C/C, C#,Java等多种编程语言开发经验&#xff0c;拥有高级工程师证书&#xff1b;擅长C/C、C#等开发语言&#xff0c;熟悉Java常用开发…

OneCode3.0 核心表达式技术深度剖析:从架构设计到动态扩展

一、引言&#xff1a;表达式技术在企业级框架中的核心价值 在当今快速变化的企业级应用开发中&#xff0c;动态性和灵活性已成为衡量框架优劣的关键指标。OneCode 3.0 框架作为企业级应用开发的重要工具&#xff0c;其核心表达式技术提供了一种强大的解决方案&#xff0c;使开发…

[css]旋转流光效果

实现一个矩形的旋转流光边框效果。 需要使用css属性梯度渐变&#xff1a;链接: conic-gradient&#xff0c;他指的是圆锥形变化的梯度。 // html<div class"demo"></div>// css body {width: 100%;height: 100%;background-color: black; }.demo {width…

NPM组件 @0xme5war/apicli 等窃取主机敏感信息

【高危】NPM组件 0xme5war/apicli 等窃取主机敏感信息 漏洞描述 当用户安装受影响版本的 0xme5war/apicli 等NPM组件包时会窃取用户的主机名、用户名、工作目录、IP地址等信息并发送到攻击者的电报地址(botToken “7699295118:AAF6pb7t718vjHWHwFQlZOastZQYHL8IVDE”&#x…