Tauri
调用自定义命令。
当 tauri.conf.json 中的 build.withGlobalTauri 设置为 true 时,还可以使用 window.__TAURI__.tauri 访问此软件包。
类型别名
InvokeArgs
InvokeArgs:
Record<string,unknown>
命令参数。
自: 1.0.0
定义于: tauri.ts:66
函数
convertFileSrc
convertFileSrc(
filePath:string,protocol?:string):string
将设备文件路径转换为 webview 可以加载的 URL。请注意,必须将 asset: 和 https://asset.localhost 添加到 tauri.security.csp 中的 tauri.conf.json。CSP 值示例:"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost" 在图像源上使用 asset 协议。
此外,还必须将 asset 添加到 tauri.allowlist.protocol 中的 tauri.conf.json,并且必须在同一 protocol 对象上的 assetScope 数组中定义其访问范围。
示例
import { appDataDir, join } from '@tauri-apps/api/path';
import { convertFileSrc } from '@tauri-apps/api/tauri';
const appDataDirPath = await appDataDir();
const filePath = await join(appDataDirPath, 'assets/video.mp4');
const assetUrl = convertFileSrc(filePath);
const video = document.getElementById('my-video');
const source = document.createElement('source');
source.type = 'video/mp4';
source.src = assetUrl;
video.appendChild(source);
video.load();
自: 1.0.0
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
filePath | string | undefined | 文件路径。 |
protocol | string | 'asset' | 要使用的协议。默认为 asset。仅在使用自定义协议时才需要设置此项。 |
返回:string
可以在 webview 上用作源的 URL。
invoke
invoke<
T>(cmd:string,args?:InvokeArgs):Promise<T>
向后端发送消息。
示例
import { invoke } from '@tauri-apps/api/tauri';
await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });
自: 1.0.0
类型参数
T
参数
| 名称 | 类型 | 说明 |
|---|---|---|
cmd | string | 命令名称。 |
args | InvokeArgs | 要传递给命令的可选参数。 |
返回:Promise<T>
解析或拒绝后端响应的 Promise。
transformCallback
transformCallback(
callback?:fn,once?:boolean):number
将回调函数转换为可传递给后端的字符串标识符。后端使用标识符对回调执行 eval()。
自: 1.0.0
参数
| 名称 | 类型 | 默认值 |
|---|---|---|
callback? | (response: any) => void | undefined |
once | boolean | false |
返回:number
与回调函数关联的唯一标识符。