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

使用效果

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

相关推荐
Jinkxs11 小时前
Rust 性能优化全流程:从 flamegraph 定位瓶颈到 unsafe 与 SIMD 加速,响应快 2 倍
开发语言·性能优化·rust
星栈独行14 小时前
Rust + Makepad 应用怎么打包发布:Windows、macOS、Linux 全平台交付
windows·程序人生·macos·ui·rust
fox_lht1 天前
15.3.改进我们之前的输入、输出项目
开发语言·后端·学习·rust
guyoung1 天前
BoxAgnts 工具系统(6)——多 Provider 适配与 Agent 查询循环
rust·agent·ai编程
星栈1 天前
Rust + Makepad 应用怎么打包发布:Windows、macOS、Linux 全平台交付
前端·rust
MageGojo1 天前
R-Shell开源项目实战解析:用Rust打造命令行SSH工具,支持连接管理、远程执行、SFTP与MCP
运维·rust·开源项目·命令行工具·ssh客户端·mcp
techdashen1 天前
Cargo 1.94 开发周期全解析
开发语言·后端·rust
fox_lht1 天前
15.4.循环和迭代器的性能比较
开发语言·后端·学习·rust
guyoung2 天前
BoxAgnts 工具系统(5)——WASM 工具开发:从 Hello World 到生产部署
rust·agent·ai编程
星栈2 天前
写 Makepad Demo 不难,难的是把它写成项目
前端·rust