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

使用效果

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

相关推荐
初学者,亦行者8 小时前
Rayon并行迭代器:原理、实践与性能优化
java·开发语言·spring·rust
RustCoder10 小时前
Rust 1.91 发布:ARM Windows 正式跻身顶级支持,悬空指针警告上线
后端·性能优化·rust
Kapaseker11 小时前
深入 Rust 迭代器(中)
rust
muyouking1112 小时前
Rust + WASM + Svelte 深度实战:内存管理、性能权衡与图像处理进阶
开发语言·rust·wasm
2301_7951672012 小时前
玩转Rust高级应用 如何进行面向对象设计模式的实现,实现状态模式
设计模式·rust·状态模式
2301_7965125212 小时前
Rust编程学习 - 如何快速构建一个单线程 web server
前端·学习·rust
想不明白的过度思考者21 小时前
Rust——异步递归深度指南:从问题到解决方案
开发语言·后端·rust
逻极1 天前
Rust数据类型(上):标量类型全解析
开发语言·后端·rust
Zhangzy@1 天前
Rust 编译优化选项
android·开发语言·rust
百锦再1 天前
第2章 第一个Rust程序
java·开发语言·后端·rust·eclipse·tomcat·hibernate