跳到内容
Tauri

对话框

用于打开和保存文件的本机系统对话框以及消息对话框。

支持的平台

此插件需要 Rust 版本至少为 1.77.2

平台 级别 注释
windows
linux
macos
android

不支持文件夹选择器

ios

不支持文件夹选择器

设置

安装 dialog 插件即可开始。

使用项目的包管理器添加依赖项

npm run tauri add dialog

用法

dialog 插件在 JavaScript 和 Rust 中均可用。以下是如何使用它

在 JavaScript 中

在 Rust 中

JavaScript

请参阅 JavaScript API 参考中的所有对话框选项

创建“是/否”对话框

显示带有 YesNo 按钮的问题对话框。

import { ask } from '@tauri-apps/plugin-dialog';
// when using `"withGlobalTauri": true`, you may use
// const { ask } = window.__TAURI__.dialog;
// Create a Yes/No dialog
const answer = await ask('This action cannot be reverted. Are you sure?', {
title: 'Tauri',
kind: 'warning',
});
console.log(answer);
// Prints boolean to the console

创建“确定/取消”对话框

显示带有 OkCancel 按钮的问题对话框。

import { confirm } from '@tauri-apps/plugin-dialog';
// when using `"withGlobalTauri": true`, you may use
// const { confirm } = window.__TAURI__.dialog;
// Creates a confirmation Ok/Cancel dialog
const confirmation = await confirm(
'This action cannot be reverted. Are you sure?',
{ title: 'Tauri', kind: 'warning' }
);
console.log(confirmation);
// Prints boolean to the console

创建消息对话框

显示带有 Ok 按钮的消息对话框。请记住,如果用户关闭对话框,它将返回 false

import { message } from '@tauri-apps/plugin-dialog';
// when using `"withGlobalTauri": true`, you may use
// const { message } = window.__TAURI__.dialog;
// Shows message
await message('File not found', { title: 'Tauri', kind: 'error' });

打开文件选择器对话框

打开文件/目录选择对话框。

multiple 选项控制对话框是否允许多选,而 directory 选项控制是否为目录选择。

import { open } from '@tauri-apps/plugin-dialog';
// when using `"withGlobalTauri": true`, you may use
// const { open } = window.__TAURI__.dialog;
// Open a dialog
const file = await open({
multiple: false,
directory: false,
});
console.log(file);
// Prints file path or URI

保存到文件对话框

打开文件/目录保存对话框。

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 .jpeg
const path = await save({
filters: [
{
name: 'My Filter',
extensions: ['png', 'jpeg'],
},
],
});
console.log(path);
// Prints the chosen path

Rust

请参阅 Rust API 参考 以查看所有可用选项。

构建询问对话框

显示带有 AbsolutelyTotally 按钮的问题对话框。

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,
});

构建消息对话框

显示带有 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,
});

构建文件选择器对话框

选择文件

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
});

默认权限

此权限集配置对话框插件中可用的对话框类型。

已授予的权限

所有对话框类型均已启用。

此默认权限集包括以下内容

  • allow-ask
  • allow-confirm
  • allow-message
  • allow-save
  • allow-open

权限表

标识符 描述

dialog:allow-ask

启用 ask 命令,无需任何预配置的作用域。

dialog:deny-ask

拒绝 ask 命令,无需任何预配置的作用域。

dialog:allow-confirm

启用 confirm 命令,无需任何预配置的作用域。

dialog:deny-confirm

拒绝 confirm 命令,无需任何预配置的作用域。

dialog:allow-message

启用 message 命令,无需任何预配置的作用域。

dialog:deny-message

拒绝 message 命令,无需任何预配置的作用域。

dialog:allow-open

启用 open 命令,无需任何预配置的作用域。

dialog:deny-open

拒绝 open 命令,无需任何预配置的作用域。

dialog:allow-save

启用 save 命令,无需任何预配置的作用域。

dialog:deny-save

拒绝 save 命令,无需任何预配置的作用域。


© 2025 Tauri Contributors. CC-BY / MIT