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.conf.json中的tauri.security.csp。示例 CSP 值:“csp”: “default-src ‘self’; img-src ‘self’ asset: https://asset.localhost” 用于在图像源上使用 asset 协议。
此外,必须将asset添加到tauri.conf.json中的tauri.allowlist.protocol,并且必须在同一个protocol对象的assetScope数组上定义其访问范围。例如
{
"tauri": {
"allowlist": {
"protocol": {
"asset": true,
"assetScope": ["$APPDATA/assets/*"]
}
}
}
}
示例
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
与回调函数关联的唯一标识符。