Skip to content
Tauri 中文网

从 Tauri 1.0 升级

本指南引导你将 Tauri 1.0 应用升级到 Tauri 2.0。

¥This guide walks you through upgrading your Tauri 1.0 application to Tauri 2.0.

准备移动

¥Preparing for Mobile

Tauri 的移动界面要求你的项目输出共享库。如果你将现有应用定位到移动设备,则必须更改你的包以生成该类型的工件以及桌面可执行文件。

¥The mobile interface of Tauri requires your project to output a shared library. If you are targeting mobile for your existing application, you must change your crate to produce that kind of artifact along with the desktop executable.

  1. 更改 Cargo 清单以生成库。附加以下块:

    ¥Change the Cargo manifest to produce the library. Append the following block:

src-tauri/Cargo.toml
[lib]
name = "app_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
  1. src-tauri/src/main.rs 重命名为 src-tauri/src/lib.rs。此文件将由桌面和移动目标共享。

    ¥Rename src-tauri/src/main.rs to src-tauri/src/lib.rs. This file will be shared by both desktop and mobile targets.

  2. lib.rs 中的 main 函数标头重命名为以下内​​容:

    ¥Rename the main function header in lib.rs to the following:

src-tauri/src/lib.rs
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
// your code here
}

tauri::mobile_entry_point 宏准备在移动设备上执行你的函数。

¥The tauri::mobile_entry_point macro prepares your function to be executed on mobile.

  1. 重新创建调用共享运行函数的 main.rs 文件:

    ¥Recreate the main.rs file calling the shared run function:

src-tauri/src/main.rs
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
app_lib::run();
}

自动迁移

¥Automated Migration

:::danger 危险

此命令不能替代本指南!无论你是否选择使用该命令,请阅读整个页面。

¥This command is not a substitude for this guide! Please read the whole page regardless of whether you chose to use the command.

:::

Tauri v2 CLI 包含一个 migrate 命令,该命令可自动执行大部分流程并帮助你完成迁移:

¥The Tauri v2 CLI includes a migrate command that automates most of the process and helps you finish the migration:

npm install @tauri-apps/cli@latest
npm run tauri migrate

了解有关 命令行接口参考migrate 命令的更多信息

¥Learn more about the migrate command in the Command Line Interface reference

变更摘要

¥Summary of Changes

以下是 Tauri 1.0 到 Tauri 2.0 的变更摘要:

¥Below is a summary of the changes from Tauri 1.0 to Tauri 2.0:

Tauri 配置

¥Tauri Configuration

  • package > productNamepackage > version 移至顶层对象。

    ¥package > productName and package > version moved to top-level object.

  • 二进制名称不再重命名为自动匹配 productName,因此你必须将 mainBinaryName 字符串添加到匹配 productName 的顶层对象。

    ¥the binary name is no longer renamed to match productName automatically, so you must add a mainBinaryName string to the top-level object matching productName.

  • package 已删除。

    ¥package removed.

  • tauri 键重命名为 app

    ¥tauri key renamed to app.

  • tauri > allowlist 已删除。请参阅 迁移权限

    ¥tauri > allowlist removed. Refer to Migrate Permissions.

  • tauri > allowlist > protocol > assetScope 已移至 app > security > assetProtocol > scope

    ¥tauri > allowlist > protocol > assetScope moved to app > security > assetProtocol > scope.

  • tauri > cli 已移至 plugins > cli

    ¥tauri > cli moved to plugins > cli.

  • tauri > windows > fileDropEnabled 已重命名为 app > windows > dragDropEnabled

    ¥tauri > windows > fileDropEnabled renamed to app > windows > dragDropEnabled.

  • tauri > updater > active 已删除。

    ¥tauri > updater > active removed.

  • tauri > updater > dialog 已删除。

    ¥tauri > updater > dialog removed.

  • tauri > updater 已移至 plugins > updater

    ¥tauri > updater moved to plugins > updater.

  • 已添加 bundle > createUpdaterArtifacts,使用应用更新程序时必须设置。

    ¥bundle > createUpdaterArtifacts added, must be set when using the app updater.

    • 从已分发的 v1 应用升级时将其设置为 v1Compatible。有关更多信息,请参阅 更新程序指南

      ¥set it to v1Compatible when upgrading from v1 apps that were already distributed. See the updater guide for more information.

  • tauri > systemTray 已重命名为 app > trayIcon

    ¥tauri > systemTray renamed to app > trayIcon.

  • tauri > pattern 已移至 app > security > pattern

    ¥tauri > pattern moved to app > security > pattern.

  • tauri > bundle 已移至顶层。

    ¥tauri > bundle moved top-level.

  • tauri > bundle > identifier 移至顶层对象。

    ¥tauri > bundle > identifier moved to top-level object.

  • tauri > bundle > dmg 已移至 bundle > macOS > dmg

    ¥tauri > bundle > dmg moved to bundle > macOS > dmg

  • tauri > bundle > deb 已移至 bundle > linux > deb

    ¥tauri > bundle > deb moved to bundle > linux > deb

  • tauri > bundle > appimage 已移至 bundle > linux > appimage

    ¥tauri > bundle > appimage moved to bundle > linux > appimage

  • tauri > bundle > macOS > license 已删除,改用 bundle > licenseFile

    ¥tauri > bundle > macOS > license removed, use bundle > licenseFile instead.

  • tauri > bundle > windows > wix > license 已删除,改用 bundle > licenseFile

    ¥tauri > bundle > windows > wix > license removed, use bundle > licenseFile instead.

  • tauri > bundle > windows > nsis > license 已删除,改用 bundle > licenseFile

    ¥tauri > bundle > windows > nsis > license removed, use bundle > licenseFile instead.

  • tauri > bundle > windows > webviewFixedRuntimePath 已删除,改用 bundle > windows > webviewInstallMode

    ¥tauri > bundle > windows > webviewFixedRuntimePath removed, use bundle > windows > webviewInstallMode instead.

  • build > withGlobalTauri 已移至 app > withGlobalTauri

    ¥build > withGlobalTauri moved to app > withGlobalTauri.

  • build > distDir 已重命名为 frontendDist

    ¥build > distDir renamed to frontendDist.

  • build > devPath 已重命名为 devUrl

    ¥build > devPath renamed to devUrl.

Tauri 2.0 配置 API 参考

¥Tauri 2.0 Configuration API reference

新的 Cargo 功能

¥New Cargo Features

  • linux-protocol-body:启用自定义协议请求正文解析,允许 IPC 使用它。需要 webkit2gtk 2.40。

    ¥linux-protocol-body: Enables custom protocol request body parsing, allowing the IPC to use it. Requires webkit2gtk 2.40.

已删除的 Cargo 功能

¥Removed Cargo Features

  • reqwest-client:reqwest 现在是唯一受支持的客户端。

    ¥reqwest-client: reqwest is now the only supported client.

  • reqwest-native-tls-vendored:改用 native-tls-vendored

    ¥reqwest-native-tls-vendored: use native-tls-vendored instead.

  • process-command-api:改用 shell 插件(请参阅以下部分中的说明)。

    ¥process-command-api: use the shell plugin instead (see instructions in the following section).

  • shell-open-api:改用 shell 插件(请参阅以下部分中的说明)。

    ¥shell-open-api: use the shell plugin instead (see instructions in the following section).

  • windows7-compat:移至 notification 插件。

    ¥windows7-compat: moved to the notification plugin.

  • 更新程序:更新程序现在是一个插件。

    ¥updater: Updater is now a plugin.

  • linux-protocol-headers:由于我们升级了最低 webkit2gtk 版本,因此现在默认启用。

    ¥linux-protocol-headers: Now enabled by default since we upgraded our minimum webkit2gtk version.

  • system-tray:重命名为 tray-icon

    ¥system-tray: renamed to tray-icon.

Rust Crate 更改

¥Rust Crate Changes

  • api 模块已删除。每个 API 模块都可以在 Tauri 插件中找到。

    ¥api module removed. Each API module can be found in a Tauri plugin.

  • api::dialog 模块已删除。改用 tauri-plugin-dialog迁移

    ¥api::dialog module removed. Use tauri-plugin-dialog instead. Migration

  • api::file 模块已删除。改用 Rust 的 std::fs

    ¥api::file module removed. Use Rust’s std::fs instead.

  • api::http 模块已删除。改用 tauri-plugin-http迁移

    ¥api::http module removed. Use tauri-plugin-http instead. Migration

  • api::ip 模块重写并移至 tauri::ipc。查看新 API,特别是 tauri::ipc::Channel

    ¥api::ip module rewritten and moved to tauri::ipc. Check out the new APIs, specially tauri::ipc::Channel.

  • api::path 模块功能和 tauri::PathResolved 移至 tauri::Manager::path迁移

    ¥api::path module functions and tauri::PathResolved moved to tauri::Manager::path. Migration

  • api::process::Commandtauri::api::shelltauri::Manager::shell_scope API 已删除。改用 tauri-plugin-shell迁移

    ¥api::process::Command, tauri::api::shell and tauri::Manager::shell_scope APIs removed. Use tauri-plugin-shell instead. Migration

  • api::process::current_binarytauri::api::process::restart 移至 tauri::process

    ¥api::process::current_binary and tauri::api::process::restart moved to tauri::process.

  • api::version 模块已被删除。改用 semver 板条箱

    ¥api::version module has been removed. Use the semver crate instead.

  • App::clipboard_managerAppHandle::clipboard_manager 已删除。改用 tauri-plugin-clipboard迁移

    ¥App::clipboard_manager and AppHandle::clipboard_manager removed. Use tauri-plugin-clipboard instead. Migration

  • App::get_cli_matches 已删除。改用 tauri-plugin-cli迁移

    ¥App::get_cli_matches removed. Use tauri-plugin-cli instead. Migration

  • App::global_shortcut_managerAppHandle::global_shortcut_manager 已删除。改用 tauri-plugin-global-shortcut迁移

    ¥App::global_shortcut_manager and AppHandle::global_shortcut_manager removed. Use tauri-plugin-global-shortcut instead. Migration

  • Manager::fs_scope 已删除。文件系统范围可以通过 tauri_plugin_fs::FsExt 访问。

    ¥Manager::fs_scope removed. The file system scope can be accessed via tauri_plugin_fs::FsExt.

  • Plugin::PluginApi 现在接收插件配置作为第二个参数。

    ¥Plugin::PluginApi now receives a plugin configuration as a second argument.

  • Plugin::setup_with_config 已删除。改用更新的 tauri::Plugin::PluginApi

    ¥Plugin::setup_with_config removed. Use the updated tauri::Plugin::PluginApi instead.

  • scope::ipc::RemoteDomainAccessScope::enable_tauri_apiscope::ipc::RemoteDomainAccessScope::enables_tauri_api 已删除。改为通过 scope::ipc::RemoteDomainAccessScope::add_plugin 单独启用每个核心插件。

    ¥scope::ipc::RemoteDomainAccessScope::enable_tauri_api and scope::ipc::RemoteDomainAccessScope::enables_tauri_api removed. Enable each core plugin individually via scope::ipc::RemoteDomainAccessScope::add_plugin instead.

  • scope::IpcScope 已删除,改用 scope::ipc::Scope

    ¥scope::IpcScope removed, use scope::ipc::Scope instead.

  • scope::FsScopescope::GlobPatternscope::FsScopeEvent 已删除,分别使用 scope::fs::Scopescope::fs::Patternscope::fs::Event

    ¥scope::FsScope, scope::GlobPattern and scope::FsScopeEvent removed, use scope::fs::Scope, scope::fs::Pattern and scope::fs::Event respectively.

  • updater 模块已删除。改用 tauri-plugin-updater迁移

    ¥updater module removed. Use tauri-plugin-updater instead. Migration

  • Env.args 字段已删除,请使用 Env.args_os 字段。

    ¥Env.args field has been removed, use Env.args_os field instead.

  • MenuMenuEventCustomMenuItemSubmenuWindowMenuEventMenuItemBuilder::on_menu_event API 已删除。迁移

    ¥Menu, MenuEvent, CustomMenuItem, Submenu, WindowMenuEvent, MenuItem and Builder::on_menu_event APIs removed. Migration

  • SystemTraySystemTrayHandleSystemTrayMenuSystemTrayMenuItemHandleSystemTraySubmenuMenuEntrySystemTrayMenuItem API 已删除。迁移

    ¥SystemTray, SystemTrayHandle, SystemTrayMenu, SystemTrayMenuItemHandle, SystemTraySubmenu, MenuEntry and SystemTrayMenuItem APIs removed. Migration

JavaScript API 更改

¥JavaScript API Changes

@tauri-apps/api 包不再提供非核心模块。仅导出以前的 tauri(现在为 core)、patheventwindow 模块。所有其他工具都已移至插件。

¥The @tauri-apps/api package no longer provides non-core modules. Only the previous tauri (now core), path, event and window modules are exported. All others have been moved to plugins.

  • @tauri-apps/api/tauri 模块重命名为 @tauri-apps/api/core迁移

    ¥@tauri-apps/api/tauri module renamed to @tauri-apps/api/core. Migration

  • @tauri-apps/api/cli 模块已删除。改用 @tauri-apps/plugin-cli迁移

    ¥@tauri-apps/api/cli module removed. Use @tauri-apps/plugin-cli instead. Migration

  • @tauri-apps/api/clipboard 模块已删除。改用 @tauri-apps/plugin-clipboard迁移

    ¥@tauri-apps/api/clipboard module removed. Use @tauri-apps/plugin-clipboard instead. Migration

  • @tauri-apps/api/dialog 模块已删除。改用 @tauri-apps/plugin-dialog迁移

    ¥@tauri-apps/api/dialog module removed. Use @tauri-apps/plugin-dialog instead. Migration

  • @tauri-apps/api/fs 模块已删除。改用 @tauri-apps/plugin-fs迁移

    ¥@tauri-apps/api/fs module removed. Use @tauri-apps/plugin-fs instead. Migration

  • @tauri-apps/api/global-shortcut 模块已删除。改用 @tauri-apps/plugin-global-shortcut迁移

    ¥@tauri-apps/api/global-shortcut module removed. Use @tauri-apps/plugin-global-shortcut instead. Migration

  • @tauri-apps/api/http 模块已删除。改用 @tauri-apps/plugin-http迁移

    ¥@tauri-apps/api/http module removed. Use @tauri-apps/plugin-http instead. Migration

  • @tauri-apps/api/os 模块已删除。改用 @tauri-apps/plugin-os迁移

    ¥@tauri-apps/api/os module removed. Use @tauri-apps/plugin-os instead. Migration

  • @tauri-apps/api/notification 模块已删除。改用 @tauri-apps/plugin-notification迁移

    ¥@tauri-apps/api/notification module removed. Use @tauri-apps/plugin-notification instead. Migration

  • @tauri-apps/api/process 模块已删除。改用 @tauri-apps/plugin-process迁移

    ¥@tauri-apps/api/process module removed. Use @tauri-apps/plugin-process instead. Migration

  • @tauri-apps/api/shell 模块已删除。改用 @tauri-apps/plugin-shell迁移

    ¥@tauri-apps/api/shell module removed. Use @tauri-apps/plugin-shell instead. Migration

  • @tauri-apps/api/updater 模块已删除。改用 @tauri-apps/plugin-updater 迁移

    ¥@tauri-apps/api/updater module removed. Use @tauri-apps/plugin-updater instead Migration

  • @tauri-apps/api/window 模块重命名为 @tauri-apps/api/webviewWindow迁移

    ¥@tauri-apps/api/window module renamed to @tauri-apps/api/webviewWindow. Migration

v1 插件现已发布为 @tauri-apps/plugin-<plugin-name>。以前它们可从 git 以 tauri-plugin-<plugin-name>-api 形式获得。

¥The v1 plugins are now published as @tauri-apps/plugin-<plugin-name>. Previously they were available from git as tauri-plugin-<plugin-name>-api.

环境变量更改

¥Environment Variables Changes

Tauri CLI 读取和写入的大多数环境变量都已重命名,以保持一致性和防止错误:

¥Most of the environment variables read and written by the Tauri CLI were renamed for consistency and prevention of mistakes:

  • TAURI_PRIVATE_KEY -> TAURI_SIGNING_PRIVATE_KEY

  • TAURI_KEY_PASSWORD -> TAURI_SIGNING_PRIVATE_KEY_PASSWORD

  • TAURI_SKIP_DEVSERVER_CHECK -> TAURI_CLI_NO_DEV_SERVER_WAIT

  • TAURI_DEV_SERVER_PORT -> TAURI_CLI_PORT

  • TAURI_PATH_DEPTH -> TAURI_CLI_CONFIG_DEPTH

  • TAURI_FIPS_COMPLIANT -> TAURI_BUNDLER_WIX_FIPS_COMPLIANT

  • TAURI_DEV_WATCHER_IGNORE_FILE -> TAURI_CLI_WATCHER_IGNORE_FILENAME

  • TAURI_TRAY -> TAURI_LINUX_AYATANA_APPINDICATOR

  • TAURI_APPLE_DEVELOPMENT_TEAM -> APPLE_DEVELOPMENT_TEAM

  • TAURI_PLATFORM -> TAURI_ENV_PLATFORM

  • TAURI_ARCH -> TAURI_ENV_ARCH

  • TAURI_FAMILY -> TAURI_ENV_FAMILY

  • TAURI_PLATFORM_VERSION -> TAURI_ENV_PLATFORM_VERSION

  • TAURI_PLATFORM_TYPE -> TAURI_ENV_PLATFORM_TYPE

  • TAURI_DEBUG -> TAURI_ENV_DEBUG

事件系统

¥Event System

事件系统经过重新设计,更易于使用。它现在不再依赖事件源,而是有一个更简单的实现,依赖于事件目标。

¥The event system was redesigned to be easier to use. Instead of relying on the source of the event, it now has a simpler implementation that relies on event targets.

  • emit 函数现在将事件发送给所有事件监听器。

    ¥The emit function now emits the event to all event listeners.

  • 添加了新的 emit_to/emitTo 函数来触发特定目标的事件。

    ¥Added a new emit_to/emitTo function to trigger an event to a specific target.

  • emit_filter 现在基于 EventTarget 而不是窗口进行过滤。

    ¥emit_filter now filters based on EventTarget instead of a window.

  • listen_global 重命名为 listen_any。它现在会监听所有事件,无论它们的过滤器和目标如何。

    ¥Renamed listen_global to listen_any. It now listens to all events regardless of their filters and targets.

  • JavaScript:event.listen() 的行为与 listen_any 类似。它现在会监听所有事件,无论它们的过滤器和目标如何,除非在 Options 中设置了目标。

    ¥JavaScript: event.listen() behaves similar to listen_any. It now listens to all events regardless of their filters and targets, unless a target is set in the Options.

  • JavaScript:WebviewWindow.listen 等仅监听发送到相应 EventTarget 的事件。

    ¥JavaScript: WebviewWindow.listen etc. only listen to events emitted to the respective EventTarget.

多 webview 支持

¥Multiwebview support

Tauri v2 引入了多 Web 视图支持,目前位于 unstable 功能标志后面。为了支持它,我们将 Rust Window 类型重命名为 WebviewWindow,将 Manager get_window 函数重命名为 get_webview_window

¥Tauri v2 introduces multiwebview support currently behind an unstable feature flag. In order to support it, we renamed the Rust Window type to WebviewWindow and the Manager get_window function to get_webview_window.

WebviewWindow JS API 类型现在从 @tauri-apps/api/webviewWindow 而不是 @tauri-apps/api/window 重新导出。

¥The WebviewWindow JS API type is now re-exported from @tauri-apps/api/webviewWindow instead of @tauri-apps/api/window.

Windows 上的新原始 URL

¥New origin URL on Windows

在 Windows 上,生产应用中的前端文件现在托管在 http://tauri.localhost 而不是 https://tauri.localhost 上。因此,除非在 v1 中使用 dangerousUseHttpScheme,否则 IndexedDB、LocalStorage 和 Cookies 将被重置。为了防止这种情况,你可以将 app > windows > useHttpsScheme 设置为 true 或使用 WebviewWindowBuilder::use_https_scheme 继续使用 https 方案。

¥On Windows the frontend files in production apps are now hosted on http://tauri.localhost instead of https://tauri.localhost. Because of this IndexedDB, LocalStorage and Cookies will be reset unless dangerousUseHttpScheme was used in v1. To prevent this you can set app > windows > useHttpsScheme to true or use WebviewWindowBuilder::use_https_scheme to keep using the https scheme.

详细迁移步骤

¥Detailed Migration Steps

将 Tauri 1.0 应用迁移到 Tauri 2.0 时可能会遇到的常见场景。

¥Common scenarios you may encounter when migrating your Tauri 1.0 app to Tauri 2.0.

迁移到核心模块

¥Migrate to Core Module

@tauri-apps/api/tauri 模块已重命名为 @tauri-apps/api/core。只需重命名模块导入:

¥The @tauri-apps/api/tauri module was renamed to @tauri-apps/api/core. Simply rename the module import:

import { invoke } from "@tauri-apps/api/tauri"
import { invoke } from "@tauri-apps/api/core"

迁移到 CLI 插件

¥Migrate to CLI Plugin

Rust App::get_cli_matches JavaScript @tauri-apps/api/cli API 已被删除。改用 @tauri-apps/plugin-cli 插件:

¥The Rust App::get_cli_matches JavaScript @tauri-apps/api/cli APIs have been removed. Use the @tauri-apps/plugin-cli plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-cli = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_cli::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-cli": "^2.0.0"
}
}
import { getMatches } from '@tauri-apps/plugin-cli';
const matches = await getMatches();

迁移到剪贴板插件

¥Migrate to Clipboard Plugin

Rust App::clipboard_managerAppHandle::clipboard_manager 以及 JavaScript @tauri-apps/api/clipboard API 已被删除。改用 @tauri-apps/plugin-clipboard-manager 插件:

¥The Rust App::clipboard_manager and AppHandle::clipboard_manager and JavaScript @tauri-apps/api/clipboard APIs have been removed. Use the @tauri-apps/plugin-clipboard-manager plugin instead:

[dependencies]
tauri-plugin-clipboard-manager = "2"
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_clipboard_manager::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-clipboard-manager": "^2.0.0"
}
}
import { writeText, readText } from '@tauri-apps/plugin-clipboard-manager';
await writeText('Tauri is awesome!');
assert(await readText(), 'Tauri is awesome!');

迁移到对话框插件

¥Migrate to Dialog Plugin

Rust tauri::api::dialog JavaScript @tauri-apps/api/dialog API 已被删除。改用 @tauri-apps/plugin-dialog 插件:

¥The Rust tauri::api::dialog JavaScript @tauri-apps/api/dialog APIs have been removed. Use the @tauri-apps/plugin-dialog plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-dialog = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-dialog": "^2.0.0"
}
}
import { save } from '@tauri-apps/plugin-dialog';
const filePath = await save({
filters: [
{
name: 'Image',
extensions: ['png', 'jpeg'],
},
],
});

迁移到文件系统插件

¥Migrate to File System Plugin

Rust App::get_cli_matches JavaScript @tauri-apps/api/fs API 已被删除。改用 Rust 的 std::fs 和 JavaScript 的 @tauri-apps/plugin-fs 插件:

¥The Rust App::get_cli_matches JavaScript @tauri-apps/api/fs APIs have been removed. Use the std::fs for Rust and @tauri-apps/plugin-fs plugin for JavaScript instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-fs = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_fs::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-fs": "^2.0.0"
}
}
import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';
await mkdir('db', { baseDir: BaseDirectory.AppLocalData });

某些函数和类型已重命名或删除:

¥Some functions and types have been renamed or removed:

  • Dir 枚举别名已删除,请使用 BaseDirectory

    ¥Dir enum alias removed, use BaseDirectory.

  • FileEntryFsBinaryFileOptionFsDirOptionsFsOptionsFsTextFileOptionBinaryFileContents 接口和类型别名已被删除,并替换为适合每个功能的新接口。

    ¥FileEntry, FsBinaryFileOption, FsDirOptions, FsOptions, FsTextFileOption and BinaryFileContents interfaces and type aliases have been removed and replaced with new interfaces suited for each function.

  • createDir 已重命名为 mkdir

    ¥createDir renamed to mkdir.

  • readBinaryFile 已重命名为 readFile

    ¥readBinaryFile renamed to readFile.

  • removeDir 已删除并替换为 remove

    ¥removeDir removed and replaced with remove.

  • removeFile 已删除并替换为 remove

    ¥removeFile removed and replaced with remove.

  • renameFile 已删除并替换为 rename

    ¥renameFile removed and replaced with rename.

  • writeBinaryFile 已重命名为 writeFile

    ¥writeBinaryFile renamed to writeFile.

迁移到全局快捷方式插件

¥Migrate to Global Shortcut Plugin

Rust App::global_shortcut_managerAppHandle::global_shortcut_manager 以及 JavaScript @tauri-apps/api/global-shortcut API 已被删除。改用 @tauri-apps/plugin-global-shortcut 插件:

¥The Rust App::global_shortcut_manager and AppHandle::global_shortcut_manager and JavaScript @tauri-apps/api/global-shortcut APIs have been removed. Use the @tauri-apps/plugin-global-shortcut plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]
tauri-plugin-global-shortcut = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_global_shortcut::Builder::default().build())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-global-shortcut": "^2.0.0"
}
}
import { register } from '@tauri-apps/plugin-global-shortcut';
await register('CommandOrControl+Shift+C', () => {
console.log('Shortcut triggered');
});

迁移到 HTTP 插件

¥Migrate to HTTP Plugin

Rust tauri::api::http JavaScript @tauri-apps/api/http API 已被删除。改用 @tauri-apps/plugin-http 插件:

¥The Rust tauri::api::http JavaScript @tauri-apps/api/http APIs have been removed. Use the @tauri-apps/plugin-http plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-http = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_http::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-http": "^2.0.0"
}
}
import { fetch } from '@tauri-apps/plugin-http';
const response = await fetch(
'https://raw.githubusercontent.com/tauri-apps/tauri/dev/package.json'
);

迁移到通知插件

¥Migrate to Notification Plugin

Rust tauri::api::notification JavaScript @tauri-apps/api/notification API 已被删除。改用 @tauri-apps/plugin-notification 插件:

¥The Rust tauri::api::notification JavaScript @tauri-apps/api/notification APIs have been removed. Use the @tauri-apps/plugin-notification plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-notification = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_notification::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-notification": "^2.0.0"
}
}
import { sendNotification } from '@tauri-apps/plugin-notification';
sendNotification('Tauri is awesome!');

迁移到菜单模块

¥Migrate to Menu Module

Rust Menu API 已移至 tauri::menu 模块并重构以使用 muda 板条箱

¥The Rust Menu APIs were moved to the tauri::menu module and refactored to use the muda crate.

使用 tauri::menu::MenuBuilder

¥Use tauri::menu::MenuBuilder

使用 tauri::menu::MenuBuilder 代替 tauri::Menu。请注意,其构造函数以 Manager 实例(AppAppHandleWebviewWindow 之一)作为参数:

¥Use tauri::menu::MenuBuilder instead of tauri::Menu. Note that its constructor takes a Manager instance (one of App, AppHandle or WebviewWindow) as an argument:

use tauri::menu::MenuBuilder;
tauri::Builder::default()
.setup(|app| {
let menu = MenuBuilder::new(app)
.copy()
.paste()
.separator()
.undo()
.redo()
.text("open-url", "Open URL")
.check("toggle", "Toggle")
.icon("show-app", "Show App", app.default_window_icon().cloned().unwrap())
.build()?;
Ok(())
})

使用 tauri::menu::PredefinedMenuItem

¥Use tauri::menu::PredefinedMenuItem

使用 tauri::menu::PredefinedMenuItem 代替 tauri::MenuItem

¥Use tauri::menu::PredefinedMenuItem instead of tauri::MenuItem:

use tauri::menu::{MenuBuilder, PredefinedMenuItem};
tauri::Builder::default()
.setup(|app| {
let menu = MenuBuilder::new(app).item(&PredefinedMenuItem::copy(app)?).build()?;
Ok(())
})

:::tip 提示

菜单构建器有专用的方法来添加每个预定义菜单项,因此你可以调用 .copy() 而不是 .item(&PredefinedMenuItem::copy(app, None)?)

¥The menu builder has dedicated methods to add each predefined menu item so you can call .copy() instead of .item(&PredefinedMenuItem::copy(app, None)?).

:::

使用 tauri::menu::MenuItemBuilder

¥Use tauri::menu::MenuItemBuilder

使用 tauri::menu::MenuItemBuilder 代替 tauri::CustomMenuItem

¥Use tauri::menu::MenuItemBuilder instead of tauri::CustomMenuItem:

use tauri::menu::MenuItemBuilder;
tauri::Builder::default()
.setup(|app| {
let toggle = MenuItemBuilder::new("Toggle").accelerator("Ctrl+Shift+T").build(app)?;
Ok(())
})

使用 tauri::menu::SubmenuBuilder

¥Use tauri::menu::SubmenuBuilder

使用 tauri::menu::SubmenuBuilder 代替 tauri::Submenu

¥Use tauri::menu::SubmenuBuilder instead of tauri::Submenu:

use tauri::menu::{MenuBuilder, SubmenuBuilder};
tauri::Builder::default()
.setup(|app| {
let submenu = SubmenuBuilder::new(app, "Sub")
.text("Tauri")
.separator()
.check("Is Awesome")
.build()?;
let menu = MenuBuilder::new(app).item(&submenu).build()?;
Ok(())
})

tauri::Builder::menu 现在采用闭包,因为菜单需要构建 Manager 实例。有关更多信息,请参阅 文档

¥tauri::Builder::menu now takes a closure because the menu needs a Manager instance to be built. See the documentation for more information.

菜单事件

¥Menu Events

Rust tauri::Builder::on_menu_event API 已被删除。改用 tauri::App::on_menu_eventtauri::AppHandle::on_menu_event

¥The Rust tauri::Builder::on_menu_event API was removed. Use tauri::App::on_menu_event or tauri::AppHandle::on_menu_event instead:

use tauri::menu::{CheckMenuItemBuilder, MenuBuilder, MenuItemBuilder};
tauri::Builder::default()
.setup(|app| {
let toggle = MenuItemBuilder::with_id("toggle", "Toggle").build(app)?;
let check = CheckMenuItemBuilder::new("Mark").build(app)?;
let menu = MenuBuilder::new(app).items(&[&toggle, &check]).build()?;
app.set_menu(menu)?;
app.on_menu_event(move |app, event| {
if event.id() == check.id() {
println!("`check` triggered, do something! is checked? {}", check.is_checked().unwrap());
} else if event.id() == "toggle" {
println!("toggle triggered!");
}
});
Ok(())
})

请注意,有两种方法可以检查选择了哪个菜单项:将项目移动到事件处理程序闭包并比较 ID,或者通过 with_id 构造函数为项目定义自定义 ID 并使用该 ID 字符串进行比较。

¥Note that there are two ways to check which menu item was selected: move the item to the event handler closure and compare IDs, or define a custom ID for the item through the with_id constructor and use that ID string to compare.

:::tip 提示

菜单项可以在菜单之间共享,并且菜单事件绑定到菜单项而不是菜单或窗口。如果你不希望在选择菜单项时触发所有监听器,请不要共享菜单项,而是使用专用实例,你可以进入 tauri::WebviewWindow/WebviewWindowBuilder::on_menu_event 闭包。

¥Menu items can be shared across menus, and the menu event is bound to a menu item instead of a menu or window. If you don’t want all listeners to be triggered when a menu item is selected, do not share menu items and use dedicated instances instead, that you could move into tauri::WebviewWindow/WebviewWindowBuilder::on_menu_event closure.

:::

迁移到操作系统插件

¥Migrate to OS Plugin

Rust tauri::api::os JavaScript @tauri-apps/api/os API 已被删除。改用 @tauri-apps/plugin-os 插件:

¥The Rust tauri::api::os JavaScript @tauri-apps/api/os APIs have been removed. Use the @tauri-apps/plugin-os plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-os = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_os::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-os": "^2.0.0"
}
}
import { arch } from '@tauri-apps/plugin-os';
const architecture = await arch();

迁移到进程插件

¥Migrate to Process Plugin

Rust tauri::api::process JavaScript @tauri-apps/api/process API 已被删除。改用 @tauri-apps/plugin-process 插件:

¥The Rust tauri::api::process JavaScript @tauri-apps/api/process APIs have been removed. Use the @tauri-apps/plugin-process plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-process = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_process::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-process": "^2.0.0"
}
}
import { exit, relaunch } from '@tauri-apps/plugin-process';
await exit(0);
await relaunch();

迁移到 Shell 插件

¥Migrate to Shell Plugin

Rust tauri::api::shell JavaScript @tauri-apps/api/shell API 已被删除。改用 @tauri-apps/plugin-shell 插件:

¥The Rust tauri::api::shell JavaScript @tauri-apps/api/shell APIs have been removed. Use the @tauri-apps/plugin-shell plugin instead:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

Cargo.toml
[dependencies]
tauri-plugin-shell = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-shell": "^2.0.0"
}
}
import { Command, open } from '@tauri-apps/plugin-shell';
const output = await Command.create('echo', 'message').execute();
await open('https://github.com/tauri-apps/tauri');

迁移到托盘图标模块

¥Migrate to Tray Icon Module

Rust SystemTray API 已重命名为 TrayIcon 以保持一致性。新的 API 可以在 Rust tray 模块中找到。

¥The Rust SystemTray APIs were renamed to TrayIcon for consistency. The new APIs can be found in the Rust tray module.

使用 tauri::tray::TrayIconBuilder

¥Use tauri::tray::TrayIconBuilder

使用 tauri::tray::TrayIconBuilder 代替 tauri::SystemTray

¥Use tauri::tray::TrayIconBuilder instead of tauri::SystemTray:

let tray = tauri::tray::TrayIconBuilder::with_id("my-tray").build(app)?;

有关更多信息,请参阅 TrayIconBuilder

¥See TrayIconBuilder for more information.

迁移到菜单

¥Migrate to Menu

使用 tauri::menu::Menu 代替 tauri::SystemTrayMenu,使用 tauri::menu::Submenu 代替 tauri::SystemTraySubmenu,使用 tauri::menu::PredefinedMenuItem 代替 tauri::SystemTrayMenuItem

¥Use tauri::menu::Menu instead of tauri::SystemTrayMenu, tauri::menu::Submenu instead of tauri::SystemTraySubmenu and tauri::menu::PredefinedMenuItem instead of tauri::SystemTrayMenuItem.

托盘事件

¥Tray Events

tauri::SystemTray::on_event 已分为 tauri::tray::TrayIconBuilder::on_menu_eventtauri::tray::TrayIconBuilder::on_tray_icon_event

¥tauri::SystemTray::on_event have been split into tauri::tray::TrayIconBuilder::on_menu_event and tauri::tray::TrayIconBuilder::on_tray_icon_event:

use tauri::{
menu::{MenuBuilder, MenuItemBuilder},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
};
tauri::Builder::default()
.setup(|app| {
let toggle = MenuItemBuilder::with_id("toggle", "Toggle").build(app)?;
let menu = MenuBuilder::new(app).items(&[&toggle]).build()?;
let tray = TrayIconBuilder::new()
.menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() {
"toggle" => {
println!("toggle clicked");
}
_ => (),
})
.on_tray_icon_event(|tray, event| {
if let TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} = event
{
let app = tray.app_handle();
if let Some(webview_window) = app.get_webview_window("main") {
let _ = webview_window.show();
let _ = webview_window.set_focus();
}
}
})
.build(app)?;
Ok(())
})

迁移到更新程序插件

¥Migrate to Updater Plugin

Rust tauri::updater 和 JavaScript @tauri-apps/api-updater API 已被删除。要使用 @tauri-apps/plugin-updater 设置自定义更新程序目标:

¥The Rust tauri::updater and JavaScript @tauri-apps/api-updater APIs have been removed. To set a custom updater target with the @tauri-apps/plugin-updater:

  1. 添加到 Cargo 依赖:

    ¥Add to cargo dependencies:

[dependencies]
tauri-plugin-updater = "2"
  1. 在 JavaScript 或 Rust 项目中使用:

    ¥Use in JavaScript or Rust project:

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_updater::Builder::new().build())
}
package.json
{
"dependencies": {
"@tauri-apps/plugin-updater": "^2.0.0"
}
}
import { check } from '@tauri-apps/plugin-updater';
import { relaunch } from '@tauri-apps/plugin-process';
const update = await check();
if (update?.available) {
console.log(`Update to ${update.version} available! Date: ${update.date}`);
console.log(`Release notes: ${update.body}`);
await update.downloadAndInstall();
// requires the `process` plugin
await relaunch();
}

将路径迁移到 Tauri Manager

¥Migrate Path to Tauri Manager

Rust tauri::api::path 模块函数和 tauri::PathResolver 已移至 tauri::Manager::path

¥The Rust tauri::api::path module functions and tauri::PathResolver have been moved to tauri::Manager::path:

use tauri::{path::BaseDirectory, Manager};
tauri::Builder::default()
.setup(|app| {
let home_dir_path = app.path().home_dir().expect("failed to get home dir");
let path = app.path().resolve("path/to/something", BaseDirectory::Config)?;
Ok(())
})

迁移到新的 Window API

¥Migrate to new Window API

在 Rust 方面,Window 已重命名为 WebviewWindow,其构建器 WindowBuilder 现在名为 WebviewWindowBuilderWindowUrl 现在名为 WebviewUrl

¥On the Rust side, Window was renamed to WebviewWindow, its builder WindowBuilder is now named WebviewWindowBuilder and WindowUrl is now named WebviewUrl.

此外,Manager::get_window 函数已重命名为 get_webview_window,窗口的 parent_window API 已重命名为 parent_raw,以支持高级窗口父 API。

¥Additionally, the Manager::get_window function was renamed to get_webview_window and the window’s parent_window API was renamed to parent_raw to support a high level window parent API.

在 JavaScript 方面,WebviewWindow 类现在在 @tauri-apps/api/webviewWindow 路径中导出。

¥On the JavaScript side, the WebviewWindow class is now exported in the @tauri-apps/api/webviewWindow path.

onMenuClicked 函数已被删除,你可以在 JavaScript 中创建菜单时拦截菜单事件。

¥The onMenuClicked function was removed, you can intercept menu events when creating a menu in JavaScript instead.

迁移嵌入的附加文件(资源)

¥Migrate Embedded Additional Files (Resources)

在 JavaScript 方面,确保你 迁移到文件系统插件。此外,请注意 迁移权限 中对 v1 允许列表所做的更改。

¥On the JavaScript side, make sure you Migrate to File System Plugin. Additionally, note the changes made to the v1 allowlist in Migrate Permissions.

在 Rust 方面,确保你有 将路径迁移到 Tauri Manager

¥On the Rust side, make sure you Migrate Path to Tauri Manager.

迁移嵌入式外部二进制文件(Sidecar)

¥Migrate Embedded External Binaries (Sidecar)

在 Tauri v1 中,外部二进制文件及其参数在允许列表中定义。在 v2 中,使用新的权限系统。阅读 迁移权限 了解更多信息。

¥In Tauri v1, the external binaries and their arguments were defined in the allowlist. In v2, use the new permissions system. Read Migrate Permissions for more information.

在 JavaScript 方面,确保你 迁移到 Shell 插件

¥On the JavaScript side, make sure you Migrate to Shell Plugin.

在 Rust 方面,tauri::api::process API 已被删除。改用 tauri_plugin_shell::ShellExttauri_plugin_shell::process::CommandEvent API。阅读 嵌入外部二进制文件 指南以了解如何操作。

¥On the Rust side, tauri::api::process API has been removed. Use tauri_plugin_shell::ShellExt and tauri_plugin_shell::process::CommandEvent APIs instead. Read the Embedding External Binaries guide to see how.

“process-command-api” 功能标志已在 v2 中删除。因此,运行外部二进制文件不再需要在 Tauri 配置中定义此功能。

¥The “process-command-api” features flag has been removed in v2. So running the external binaries does not require this feature to be defined in the Tauri config anymore.

迁移权限

¥Migrate Permissions

v1 允许列表已被重写为一个全新的权限系统,该系统适用于各个插件,并且更易于配置以支持多窗口和远程 URL。这个新系统的工作方式类似于访问控制列表 (ACL),你可以在其中允许或拒绝命令、为特定的一组窗口和域分配权限以及定义访问范围。

¥The v1 allowlist have been rewritten to a completely new system for permissions that works for individual plugins and is much more configurable for multiwindow and remote URL support. This new system works like an access control list (ACL) where you can allow or deny commands, allocate permissions to a specific set of windows and domains, and define access scopes.

要为你的应用启用权限,你必须在 src-tauri/capabilities 文件夹中创建功能文件,Tauri 将自动为你配置其他所有内容。

¥To enable permissions for your app, you must create capability files inside the src-tauri/capabilities folder, and Tauri will automatically configure everything else for you.

migrate CLI 命令会自动解析你的 v1 允许列表并生成关联的功能文件。

¥The migrate CLI command automatically parses your v1 allowlist and generates the associated capability file.

要了解有关权限和功能的更多信息,请参阅 安全文档

¥To learn more about permissions and capabilities, see the security documentation.


Tauri v2.3 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站