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

使用效果

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

相关推荐
Rust研习社1 小时前
Rust 的 Box、Rc、Arc 到底怎么选?
开发语言·后端·rust
x-cmd1 小时前
agent-browser 源码分析(二):WebSocket CDP 客户端
websocket·rust·cdp·json-rpc·agent-browser
x-cmd1 小时前
agent-browser 与 CDP:浏览器自动化的底层原理
rust·浏览器自动化·cdp·agent-browser·chrome devtools protocol
时空系21 小时前
认识Rust——我的第一个程序 Rust中文编程
开发语言·后端·rust
时空系1 天前
第10篇:归属权与借用——Rust的安全保障 Rust中文编程
开发语言·安全·rust
时空系1 天前
第6篇:数据容器——管理大量数据 Rust中文编程
开发语言·后端·rust
时空系1 天前
第7篇:功能——打造你的工具箱 Rust中文编程
开发语言·网络·rust
qcx231 天前
拆解 Warp AI Agent(五):跨生态联邦——10 种 Skill + MCP + 多 Harness 互操作设计
人工智能·rust·ai agent·skill·warp·mcp·harness