【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>

使用效果

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

相关推荐
班公湖里洗过脚5 小时前
Rust解析mp3文件及工作目录下多个项目维护示例
rust
sunny_16 小时前
构建工具的第三次革命:从 Rollup 到 Rust Bundler,我是如何设计 robuild 的
前端·rust·前端工程化
用户02350873731221 小时前
第02篇:5分钟上手 blockcell —— 从安装到第一次对话
rust
badmonster01 天前
我写了一个超轻量MCP,让编码Agent真正理解你的代码——Token消耗减少70%,1分钟接入
rust·ai编程
RoyLin1 天前
10美元硬件中可运行的隐私 LLM 推理引擎
人工智能·rust·agent
Source.Liu1 天前
【egui】[特殊字符] 窗口配置小抄:eframe::NativeOptions
rust·egui
Hello.Reader1 天前
Tauri 项目结构前端壳 + Rust 内核,怎么协作、怎么构建、怎么扩展
开发语言·前端·rust
Source.Liu1 天前
【egui】[特殊字符]简单、快速、跨平台的 Rust GUI 库
rust·egui
Source.Liu2 天前
【rust-i18n】Cargo.toml 配置文件解析
rust
班公湖里洗过脚2 天前
Rust操作Josn数据及工作目录下多个应用程序维护示例
rust