【Tauri2.0教程(四)】对话框插件的使用

说明

安装

使用对话框首先要安装dialog插件。

bash 复制代码
npm run tauri add dialog
# 或者(如果上面的命令不生效,就用这个)
npx tauri add dialog

代码使用

导入需要的函数并使用。对话框总共分为问询、确认、消息提示、打开文件、保存文件五种,分别对应ask, confirm, message, open, save五个函数。

js 复制代码
<script setup>
import { ask, confirm, message, open, save} from '@tauri-apps/plugin-dialog';

async function myAsk(){
  const answer = await ask('This action cannot be reverted. Are you sure?', {
    title: 'Tauri',
    kind: 'warning',
  });

  console.log(answer);
}

async function myConfirm() {
// 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);
}

async function myMessage(){
// Shows message
  await message('File not found', { title: 'Tauri', kind: 'error' });
}

async function mySave(){
// 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);
}

async function myOpen() {
  const file = await open({
    multiple: false,
    directory: false,
  });
  console.log(file);
}

</script>

在html中使用这些函数

html 复制代码
<div class="img-panel">
  <button @click="myOpen">打开</button>
  <button @click="myAsk">yes or no</button>
  <button @click="myConfirm">确认</button>
  <button @click="myMessage">message</button>
  <button @click="mySave">保存</button>
</div>

使用效果

下面是五种对话框的使用效果。

相关推荐
alwaysrun7 小时前
Rust之代数数据类型Enum
后端·rust·编程语言
疏狂难除8 小时前
随便玩玩lldb(三)
rust·lldb
右耳朵猫AI9 小时前
Rust技术周刊 2026年第19周
开发语言·后端·rust
古城小栈11 小时前
cargo-pprof:Rust性能调优
人工智能·算法·rust
Vallelonga12 小时前
Rust 中的 Atomic 类型
rust
G_dou_1 天前
Linux 搭建 Rust 开发环境:从 rustup 安装到 Cargo 镜像
linux·rust
小灰灰搞电子1 天前
Rust 实现异步ModbusTCP主机源码分享
服务器·网络·modbustcp·rust
小杍随笔1 天前
【Rust 工具链管理完全指南:rustup toolchain 命令实战详解】
开发语言·后端·rust
Vallelonga1 天前
Rust 中 unsafe 关键字的语义
开发语言·rust