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

使用效果

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

相关推荐
superman超哥18 小时前
Rust 可变借用的独占性要求:排他访问的编译期保证
开发语言·后端·rust·rust可变借用·独占性要求·排他访问·编译期保证
superman超哥18 小时前
Rust 引用的作用域与Non-Lexical Lifetimes(NLL):生命周期的精确革命
开发语言·后端·rust·生命周期·编程语言·rust引用的作用域·rust nll
古城小栈19 小时前
Rust 生命周期,三巨头之一
开发语言·后端·rust
木木木一21 小时前
Rust学习记录--C3 Rust通用编程概念
开发语言·学习·rust
superman超哥1 天前
Rust 所有权与零成本抽象的关系:编译期优化的完美结合
开发语言·后端·rust·rust所有权·rust零成本抽象·编译期优化
古城小栈1 天前
Rust 是面向对象的语言吗?
rust
superman超哥1 天前
Rust 所有权系统如何防止双重释放:编译期的内存安全保证
开发语言·后端·rust·编程语言·内存安全·rust所有权·双重释放
superman超哥1 天前
Rust Drop Trait 与资源清理机制:确定性析构的优雅实现
开发语言·后端·rust·编程语言·rust drop trait·资源清理机制·确定性析构
superman超哥1 天前
Rust 部分移动(Partial Move)的使用场景:精细化所有权管理的艺术
开发语言·后端·rust·所有权管理·rust部分移动·partial move
superman超哥1 天前
Rust 借用检查器的工作原理:编译期内存安全的守护者
开发语言·后端·rust·编程语言·rust借用检查器·编译期内存安全·借用检查器