惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

卖坚果的怪叔叔

上线8年,15万用户的小程序,一共赚了多少钱? 糟糕的三月 还未见面的小狗,去天堂了。 快过年了!你们放假通知下来了吗? 免费领取一年Gemini Pro使用权益!! 2025年,如梦一场! 西安! WordPress评论留言通知推送插件! 初冬!暖日!东太湖慢行! H5在safari上fixed不触底bug? 2025年国庆的一些记录! 秋末的晚上 我用Trae做了一个一键同步热点要闻到公众号的工具 使用uview-plus遇到的一些问题! Nuxt3中的水合是什么?以及使用中的一些总结! 夏日的周末! 腾讯视频VIP会员免费领取!3-31天。 七月已经到中旬了呀 周末了,但也过得太快了吧。 吐血收集的1000+九号电动车提示音,免费领取! Nuxt3中piana持久化处理! 生活中的突发事件 小毛驴历险记2 小毛驴历险记 QQ,你有多久没有打开啦! npm install 出现 Error:EISDIR:illegal operation on a directory 的错误提示! 最近为啥没更新? 是过年呀 PC微信多开和防撤回~ 好险!我差点被诈骗了~
Element Plus Upload 添加支持拖拽排序~
坚果大叔 2025-07-30 20:55:55技术阅读 2,770 · 2025-07-30 · via 卖坚果的怪叔叔

项目中使用element-plus ui框架作为基础项目组件,使用了其中的upload上传组件,在后续过程中提出需求需要支持拖拽排序,我...

但是原有的 upload 组件在其他方面都支持的很好,唯独没有没有支持拖拽这个功能,没有办法只能自己二次加工了。

组件主要分为三个部分:

  1. 上传: 使用原有的upload 组件
  2. 拖拽: 使用第三方插件 draggable 来实现图片列表的拖拽效果
  3. 预览: 因为没有使用原生组件,所以自带的预览也就没有了。

1. 使用draggable并设置相应的参数

关键配置:

  • item-key="uid": 指定每个拖拽项的唯一标识符
  • :animation="200": 设置拖拽动画时长为200ms
  • ghost-class="ghost": 拖拽时的幽灵元素样式类
  • @end="onDragEnd": 拖拽结束时的回调函数

2. 拖拽template的整体结构


  

3. 拖拽事件处理

因为是使用的成熟的第三方库,所以我们不需要关系拖拽的底层逻辑,只需要在应用层做相关的处理。

// 拖拽结束处理函数
const onDragEnd = () => {
  emit("update:modelValue", fileList.value);
};

1. 数据的实时更新

const fileList = ref(props.initFiles);

// 监听文件列表变化,同步到父组件
watch(fileList, (newVal) => {
  emit("update:modelValue", newVal);
});

2. 删除已上传的文件并且更新

const handleRemove = (
  uploadFile: UploadUserFile,
  uploadFiles?: UploadUserFile[]
) => {
  if (uploadFiles) {
    fileList.value = uploadFiles;
  } else {
    // 从拖拽列表中删除
    const index = fileList.value.findIndex(
      (file) => file.uid === uploadFile.uid
    );
    if (index > -1) {
      fileList.value.splice(index, 1);
      emit('update:modelValue', fileList.value);
    }
  }
};

3. 添加上传进度展示

// 上传进度回调
(percent) => {
  const fileIndex = fileList.value.findIndex(
    (f) => f.uid === file.uid
  );
  if (fileIndex !== -1) {
    fileList.value[fileIndex] = {
      ...fileList.value[fileIndex],
      percentage: percent,
    };
  }
}

// 添加幽灵效果
.ghost {
  opacity: 0.5;
  transform: scale(0.95);
}

既然原有组件不支持相关业务功能,我们不必执着于去改造他,换个思路,可以只保留他的核心上传的功能,其他像拖拽、预览、上传进度可以通过自定义的方式来实现。

毕竟没有哪个第三方组件能完美适配每一个项目,该造的轮子还是要造的。