对话框
本机系统对话框,用于打开和保存文件,以及消息对话框。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | Does not support folder picker | |
| ios | Does not support folder picker |
安装对话框插件以开始使用。
使用项目的包管理器来添加依赖:
npm run tauri add dialogyarn run tauri add dialogpnpm tauri add dialogdeno task tauri add dialogbun tauri add dialogcargo tauri add dialog-
在
src-tauri文件夹中运行以下命令,将插件添加到项目的Cargo.toml依赖中:cargo add tauri-plugin-dialog -
修改
lib.rs来初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_dialog::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
如果你想在 JavaScript 中创建对话框,还需要安装 npm 包:
npm install @tauri-apps/plugin-dialogyarn add @tauri-apps/plugin-dialogpnpm add @tauri-apps/plugin-dialogdeno add npm:@tauri-apps/plugin-dialogbun add @tauri-apps/plugin-dialog
对话框插件可以在 JavaScript 和 Rust 中使用。你可以这样使用它:
在 JavaScript:
在 Rust:
JavaScript
Section titled “JavaScript”可以在 JavaScript API 参考中查看所有 Dialog 选项。
创建 Yes/No 对话框
Section titled “创建 Yes/No 对话框”显示一个带有 “Yes” 和 “No” 按钮的提问对话框。
import { ask } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { ask } = window.__TAURI__.dialog;
// Create a Yes/No dialogconst answer = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning',});
console.log(answer);// Prints boolean to the console创建 Ok/Cancel 对话框
Section titled “创建 Ok/Cancel 对话框”显示一个带有 “Ok” 和 “Cancel” 按钮的提问对话框。
import { confirm } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { confirm } = window.__TAURI__.dialog;
// Creates a confirmation Ok/Cancel dialogconst confirmation = await confirm( 'This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning' });
console.log(confirmation);// Prints boolean to the console创建 Message 对话框
Section titled “创建 Message 对话框”一个带有 “Ok” 按钮的消息对话框。请注意,如果用户关闭对话框,它将返回 false。
import { message } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { message } = window.__TAURI__.dialog;
// Shows messageawait message('File not found', { title: 'Tauri', kind: 'error' });打开一个文件选择对话框
Section titled “打开一个文件选择对话框”打开一个文件/目录选择对话框。
multiple 选项控制对话框是否允许多重选择,而 directory 则控制对话框是否允许目录选择。
import { open } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { open } = window.__TAURI__.dialog;
// Open a dialogconst file = await open({ multiple: false, directory: false,});console.log(file);// Prints file path or URI保存到文件对话框
Section titled “保存到文件对话框”打开一个文件/目录保存对话框。
import { save } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { save } = window.__TAURI__.dialog;
// Prompt to save a 'My Filter' with extension .png or .jpegconst path = await save({ filters: [ { name: 'My Filter', extensions: ['png', 'jpeg'], }, ],});console.log(path);// Prints the chosen path请参阅 Rust API 参考以查看所有可用选项。
建立一个询问对话框
Section titled “建立一个询问对话框”显示一个带有 “Absolutely” 和 “Totally” 按钮的问题对话框。
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
let answer = app.dialog() .message("Tauri is Awesome") .title("Tauri is Awesome") .buttons(MessageDialogButtons::OkCancelCustom("Absolutely", "Totally")) .blocking_show();如果你需要一个非阻塞操作,你可以使用 show():
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
app.dialog() .message("Tauri is Awesome") .title("Tauri is Awesome") .buttons(MessageDialogButtons::OkCancelCustom("Absolutely", "Totally")) .show(|result| match result { true => // do something, false =>// do something, });建立消息对话框
Section titled “建立消息对话框”一个带有 “Ok” 按钮的消息对话框。请注意,如果用户关闭对话框,它将返回 false。
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
let ans = app.dialog() .message("File not found") .kind(MessageDialogKind::Error) .title("Warning") .blocking_show();如果你需要一个非阻塞操作,你可以使用 show():
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};
app.dialog() .message("Tauri is Awesome") .kind(MessageDialogKind::Info) .title("Information") .buttons(MessageDialogButtons::OkCustom("Absolutely")) .show(|result| match result { true => // do something, false => // do something, });建立一个文件选择器对话框
Section titled “建立一个文件选择器对话框”use tauri_plugin_dialog::DialogExt;
let file_path = app.dialog().file().blocking_pick_file();// return a file_path `Option`, or `None` if the user closes the dialog如果你需要一个非阻塞操作,你可以使用 pick_file():
use tauri_plugin_dialog::DialogExt;
app.dialog().file().pick_file(|file_path| { // return a file_path `Option`, or `None` if the user closes the dialog })use tauri_plugin_dialog::DialogExt;
let file_path = app .dialog() .file() .add_filter("My Filter", &["png", "jpeg"]) .blocking_save_file(); // do something with the optional file path here // the file path is `None` if the user closed the dialog或者:
use tauri_plugin_dialog::DialogExt;
app.dialog() .file() .add_filter("My Filter", &["png", "jpeg"]) .pick_file(|file_path| { // return a file_path `Option`, or `None` if the user closes the dialog });Default Permission
This permission set configures the types of dialogs available from the dialog plugin.
Granted Permissions
All dialog types are enabled.
This default permission set includes the following:
allow-messageallow-saveallow-open
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the ask command without any pre-configured scope. (DEPRECATED: This is now an alias to |
|
|
Denies the ask command without any pre-configured scope. (DEPRECATED: This is now an alias to |
|
|
Enables the message command without any pre-configured scope. |
|
|
Denies the message command without any pre-configured scope. |
|
|
Enables the open command without any pre-configured scope. |
|
|
Denies the open command without any pre-configured scope. |
|
|
Enables the save command without any pre-configured scope. |
|
|
Denies the save command without any pre-configured scope. |
|
|
Enables the confirm command without any pre-configured scope. (DEPRECATED: This is now an alias to |
|
|
Denies the confirm command without any pre-configured scope. (DEPRECATED: This is now an alias to |
© 2026 Tauri Contributors. CC-BY / MIT