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

使用效果

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

相关推荐
mit6.8241 小时前
C 语言仓库引入 Rust: MCUboot 为例
开发语言·rust
星释1 小时前
Rust 练习册 99:让数字开口说话
开发语言·后端·rust
我发在否1 小时前
Rust > 牛客OJ在线编程常见输入输出练习场
算法·rust
y***54882 小时前
Rust在嵌入式中的实时操作系统
开发语言·后端·rust
苦难之路2 小时前
rCore1
rust
x***B4113 小时前
Rust unsafe代码规范
开发语言·rust·代码规范
alwaysrun3 小时前
Rust多线程编程之Thread与Channel
rust·channel·bus·mpsc·crossbeam
星释11 小时前
Rust 练习册 95:React与响应式编程
开发语言·react.js·rust
Eighteenzi12 小时前
tokio 的理解
rust
星释12 小时前
Rust 练习册 96:Rectangles与几何计算
开发语言·后端·rust