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

使用效果

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

相关推荐
古城小栈6 小时前
Rust 调用 C 语言库 实战指南(企业级)
c语言·开发语言·rust
刘布斯yy15 小时前
新写了个直播录制工具,可录制抖音快手斗鱼直播
rust·音视频·直播录制
恋喵大鲤鱼18 小时前
Rust 中的字符串 slice 是什么?
rust
迷渡2 天前
用 Rust 重写的 Bun 有 13365 个 unsafe!
开发语言·后端·rust
咸甜适中2 天前
rust语言学习笔记Trait(九)PartialEq、 Eq(相等比较)
笔记·学习·rust
恋喵大鲤鱼2 天前
Rust 中 package crate 和 module 的关系
rust
Rust研习社2 天前
Rust 官方拟定 LLM 政策,防止 LLM 污染开源社区?
开发语言·后端·ai·rust·开源
techdashen2 天前
Async Rust 近况补课:从 `async-trait` 到原生 async trait
网络·算法·rust
techdashen2 天前
在 Async Rust 中实现请求合并(Request Coalescing)
开发语言·后端·rust
咸甜适中2 天前
rust语言学习笔记Trait(八)Iterator(迭代器)
笔记·学习·rust