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

推荐订阅源

月光博客
月光博客
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
量子位
小众软件
小众软件
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
G
Google Developers Blog
博客园 - 叶小钗
H
Help Net Security
Jina AI
Jina AI
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News

姓王者的博客

Linux用户Secure Boot自主维护指南 | 姓王者的博客 MAD Bugs 已经开始——关于信息安全的军备竞赛 | 姓王者的博客 解决钉钉Dingtalk无法在Linux新版内核上启动问题-修复可执行栈错误 | 姓王者的博客 突发:GitHub 正遭受大规模 Issue 赌博广告轰炸 | 姓王者的博客 Ubuntu26.04-beta体验:坚毅浣熊! | 姓王者的博客 fakeclaw装作龙虾发贴吧 | 姓王者的博客 找回12年前的QQ记忆 | 姓王者的博客 在Linux上玩Flash网页游戏-洛克王国 | 姓王者的博客 Copilot将使用交互数据来训练 | 姓王者的博客 重要通知-请更新我的GPG公钥 | 姓王者的博客 为了自由Android | 姓王者的博客 GPL"2,3"事 | 姓王者的博客 短文-对VitePlus的一点🤏小贡献 | 姓王者的博客 Bing收录没了?亲测有效的快速恢复指南 | 姓王者的博客 解决桌面设备二维码快速识别的工具-ClipQR | 姓王者的博客 解决 Nautilus 自定义终端插件安装依赖问题 | 姓王者的博客 OpenClaw 该熄火了 | 姓王者的博客 Vite8 - 统一的基建开始 | 姓王者的博客 Astro 6 推出啦 | 姓王者的博客 ubuntu的openvpn异常暂停推送更新 | 姓王者的博客 Ubuntu 24.04 安装 Win10 虚拟机 | 姓王者的博客 ESA-后记:热爱阿里云 | 姓王者的博客 Moonbit 0.8.0 重大发布,我也要改一下我的包 | 姓王者的博客 ESA Pages 边缘开发大赛获奖 | 姓王者的博客 Astro: 优化katex,mermaid和灯箱使用 | 姓王者的博客 从edgeone迁移到esa | 姓王者的博客 出租人类:AI时代的荒诞与真实 | 姓王者的博客 Astro 5.17构建性能优化实践:从18s到13s | 姓王者的博客 Moonbit License Checker 开发使用 | 姓王者的博客 Stalux Astro博客主题自荐 | 姓王者的博客
解决Tauri2.x拖拽事件问题 | 姓王者的博客
作者:xingwangzhe · 2025-09-03 · via 姓王者的博客

🕒 阅读时间:1 分钟 📝 字数:391 👀 阅读量: Loading...

前言

最近在使用 Tauri2.x 开发桌面应用时,遇到了一个棘手的问题:拖拽事件无法正常工作。经过一番调查和尝试,终于找到了解决方案。在这篇文章中,我将分享我的解决过程,希望能帮助到遇到类似问题的开发者。

问题描述

使用前端监听拖拽事件时,发现事件无法触发。例如

document.addEventListener("DOMContentLoaded", () => {

const uploadZone = document.getElementById("file-upload-zone");

const fileList = document.getElementById("file-list");

uploadZone.addEventListener("dragover", (e) => {

e.preventDefault();

uploadZone.classList.add("drag-hover");

});

uploadZone.addEventListener("drop", (e) => {

e.preventDefault();

uploadZone.classList.remove("drag-hover");

const files = Array.from(e.dataTransfer.files);

displayFiles(files);

});

function displayFiles(files) {

fileList.innerHTML = files

.map((file) => `<div class="file-item">${file.name} (${file.size} bytes)</div>`)

.join("");

}

});

无论如何拖拽,控制台都没有任何输出。

解决方案

默认情况下,Tauri 配置

{

"app": {

"windows": [

{

"label": "main",

"title": "wngtools",

"width": 800,

"height": 600,

"resizable": true,

"fullscreen": false,

"dragDropEnabled": true //这个默认是true

//Whether the drag and drop is enabled or not on the webview. By default it is enabled.

//Disabling it is required to use HTML5 drag and drop on the frontend on Windows.

}

],

...

},

...

}

Tauri 的安全策略阻止了拖拽事件。默认情况下,前端事件被 Tauri 拦截,走它自己的事件

比如说tauri://drag-drop,tauri://drag-leave,tauri://drag-enter等。

那么方案就有两个,一个是声明"dragDropEnabled": false,这样前端的拖拽事件就能正常工作了。

但既然Tauri以权限控制与安全为主,我选择了第二个方案,使用 Tauri 提供的拖拽事件。

所以要把原有的拖拽事件改成 Tauri 的事件。

import { listen } from "@tauri-apps/api/event";

listen("tauri://drag-drop", (e) => {

console.log("Dropped files:", e);

});

listen("tauri://drag-leave", () => {

console.log("Drag leave");

});

listen("tauri://drag-enter", () => {

console.log("Drag enter");

});

当我尝试拖拽文件到应用窗口时,控制台成功输出了拖拽的文件信息。 控制台消息

当然,根据官方文档,Tauri:// 事件并不能监听到具体的 DOM 对象(当然,如果你喜欢用相对坐标来判断那我没话说),所以如果你需要监听具体的 DOM 对象的拖拽事件,还是需要把dragDropEnabled设置为false,并且使用原生的DOM拖拽事件才能行。

参考