合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] > [接口总览](https://tauri.app/v1/api/js/) ## 总览 * [app](https://tauri.app/v1/api/js/app) 获取版本信息 * [cli](https://tauri.app/v1/api/js/cli) 获取参数 * [clipboard](https://tauri.app/v1/api/js/clipboard) 剪切板 * [dialog](https://tauri.app/v1/api/js/dialog) 确认框,文件框等 * [event](https://tauri.app/v1/api/js/event) * [fs](https://tauri.app/v1/api/js/fs) * [globalShortcut](https://tauri.app/v1/api/js/globalShortcut) 全局快捷键 * [http](https://tauri.app/v1/api/js/http) 网络请求 * [mocks](https://tauri.app/v1/api/js/mocks) * [notification](https://tauri.app/v1/api/js/notification) 系统通知 * [os](https://tauri.app/v1/api/js/os) * [path](https://tauri.app/v1/api/js/path) * [process](https://tauri.app/v1/api/js/process) * [shell](https://tauri.app/v1/api/js/shell) * [tauri](https://tauri.app/v1/api/js/tauri) * [updater](https://tauri.app/v1/api/js/updater) * [window](https://tauri.app/v1/api/js/window) ## cli 示例 ``` import { getMatches } from '@tauri-apps/api/cli'; const matches = await getMatches(); if (matches.subcommand?.name === 'run') { // `./your-app run $ARGS` was executed const args = matches.subcommand?.matches.args if ('debug' in args) { // `./your-app run --debug` was executed } } else { const args = matches.args // `./your-app $ARGS` was executed } ``` ## clipboard 示例 ``` import { readText } from '@tauri-apps/api/clipboard'; const clipboardText = await readText(); ``` ## dialog 示例1 ``` import { confirm } from '@tauri-apps/api/dialog'; const confirmed = await confirm('Are you sure?', 'Tauri'); ``` 示例2: ``` import { open } from '@tauri-apps/api/dialog'; // Open a selection dialog for image files const selected = await open({ multiple: true, filters: [{ name: 'Image', extensions: ['png', 'jpeg'] }] }); if (Array.isArray(selected)) { // user selected multiple files } else if (selected === null) { // user cancelled the selection } else { // user selected a single file } ``` ## path 系统各个 常用path 路径 各个路径的实际值 ``` appCacheDir C:\Users\CPJ\AppData\Local\com.cpj.dev\ appConfigDir C:\Users\CPJ\AppData\Roaming\com.cpj.dev\ appDataDir C:\Users\CPJ\AppData\Roaming\com.cpj.dev\ appLocalDataDir C:\Users\CPJ\AppData\Local\com.cpj.dev\ appLogDir C:\Users\CPJ\AppData\Roaming\com.cpj.dev\logs\ audioDir C:\Users\CPJ\Music\ templateDir C:\Users\CPJ\AppData\Roaming\Microsoft\Windows\Templates\ desktopDir C:\Users\CPJ\Desktop\ documentDir 系统文件目录 downloadDir 系统下载目录 homeDir C:\Users\CPJ\ localDataDir C:\Users\CPJ\AppData\Local\ publicDir C:\Users\Public\ ``` 其他函数 ``` resolveResource 解析文件的绝对路径 resourceDir 当前程序的目录 extname 获取文件后缀 isAbsolute join ``` 示例 ``` import { appLocalDataDir } from '@tauri-apps/api/path'; const appLocalDataDirPath = await appLocalDataDir(); ``` ## fs ``` copyFile createDir exists readBinaryFile readDir readTextFile removeDir removeFile renameFile writeBinaryFile writeTextFile ``` ## globalShortcut 示例1: ``` import { register } from '@tauri-apps/api/globalShortcut'; await register('CommandOrControl+Shift+C', () => { console.log('Shortcut triggered'); }); ``` ## notification 示例 ``` import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/api/notification'; let permissionGranted = await isPermissionGranted(); if (!permissionGranted) { const permission = await requestPermission(); permissionGranted = permission === 'granted'; } if (permissionGranted) { sendNotification('Tauri is awesome!'); sendNotification({ title: 'TAURI', body: 'Tauri is awesome!' }); } ``` ## os ``` arch 获取当前架构 platform 获取当前平台 tempdir 获取系统的临时目录 type 系统类型 linux,Darwin,Windows_NT version 系统版本 ``` ## shell 执行外部指令 tauri.conf.json ``` "tauri": { "allowlist": { "all": true, "shell": { "all": true, "open": true, "scope": [ { "name": "run-git-commit", "cmd": "node", "args": ["--help"] } ] } }, ... "externalBin": [ "bin/node" ], ``` vue ``` import { Command } from '@tauri-apps/api/shell' async function greet() { let childProcess = await new Command('run-git-commit').execute(); console.log(childProcess); } ``` ## window ### Window events ### WebviewWindow 再打开窗口 ``` // loading embedded asset: const webview = new WebviewWindow('theUniqueLabel', { url: 'path/to/page.html' }); // alternatively, load a remote URL: const webview = new WebviewWindow('theUniqueLabel', { url: 'https://github.com/tauri-apps/tauri' }); webview.once('tauri://created', function () { // webview window successfully created }); webview.once('tauri://error', function (e) { // an error happened creating the webview window }); // emit an event to the backend await webview.emit("some event", "data"); // listen to an event from the backend const unlisten = await webview.listen("event name", e => {}); unlisten(); ```