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

使用效果

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

相关推荐
muyouking111 小时前
Zig vs Rust:常用类型声明方式对比与核心理念解析
rust
s9123601012 小时前
【rust】生成带白边的标准二维码
开发语言·后端·rust
测试人社区—小叶子4 小时前
Rust会取代C++吗?系统编程语言的新较量
运维·开发语言·网络·c++·人工智能·测试工具·rust
古城小栈4 小时前
Java 应对 Rust 竞争的 性能优化策略
java·性能优化·rust
ALex_zry4 小时前
Rust 变量遮蔽 五类典型应用场景
开发语言·后端·rust
Pomelo_刘金13 小时前
编程界 语言神 : 赶紧起来学 Rust 了!
rust
会头痛的可达鸭1 天前
Reqwest 库详细使用指南
http·rust·reqwest
mzhan0171 天前
Rust 资料
开发语言·后端·rust
Hello.Reader1 天前
Rocket 0.5 快速上手从克隆仓库到跑起第一个示例
rust·rocket
HelloReader1 天前
一款“既好玩又靠谱”的 Rust Web 框架
后端·rust