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

使用效果

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

相关推荐
盒马盒马13 小时前
Rust:内部可变性 & 型变
开发语言·后端·rust
云水木石16 小时前
Rust 语言开发的 Linux 桌面来了
linux·运维·开发语言·后端·rust
Source.Liu17 小时前
【time-rs】解释://! Invalid format description(error/invalid_format_description.rs)
rust·time
fegggye17 小时前
创建一个rust写的python库[signatures和错误处理]
开发语言·python·rust
Source.Liu17 小时前
【time-rs】解释://! Indeterminate offset(error/indeterminate_offset.rs)
rust·time
福大大架构师每日一题17 小时前
rust 1.92.0 更新详解:语言特性增强、编译器优化与全新稳定API
java·javascript·rust
分布式存储与RustFS19 小时前
MinIO替代方案精选:RustFS深度评测与选型指南
人工智能·rust·开源项目·对象存储·minio·企业存储·rustfs
JPX-NO20 小时前
使用cargo-generate自定义创建项目模板
rust·mvc
JPX-NO20 小时前
Rust Rocket Web 应用项目结构详解(MVC 风格)
rust·mvc·rocket
Source.Liu1 天前
【time-rs】DifferentVariant 错误类型详解(error/different_variant.rs)
rust·time