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

使用效果

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

相关推荐
5***g29820 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
盒马盒马21 小时前
Rust:Trait 标签 & 常见特征
开发语言·后端·rust
muyouking111 天前
Rust Slice 完全指南:从基础用法到 3D 场景实战
开发语言·3d·rust
Sammyyyyy1 天前
Rust性能调优:从劝退到真香
开发语言·后端·rust·servbay
pilaf19901 天前
Rust练习题
开发语言·后端·rust
IT笔记1 天前
【Rust】Rust数组和Vec安全读写笔记
笔记·安全·rust
Source.Liu1 天前
【Chrono库】时间区域(TimeZone)Rust实现详解(src/offset/local/tz_info/timezone.rs)
rust·time
CNRio1 天前
GitCode CLI:从Python到Rust的重构之旅
python·rust·gitcode
xcLeigh1 天前
【新】Rust入门:基础语法应用
开发语言·算法·rust
星释1 天前
Rust 练习册 103:维吉尼亚密码与安全通信
网络·安全·rust