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 中,其访问范围必须定义在同一协议对象的 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>
resolve或reject后台响应的promise。
transformCallback
transformCallback(
callback?:fn,once?:boolean):number
将回调函数转换为可传递给后台的字符串标识符。后端使用标识符来eval()回调函数。
自1.0.0版本起
参数
| 名称 | 类型 | 默认值 |
|---|---|---|
callback? |
(response: any) => void |
undefined |
once |
boolean |
false |
返回值: number
与回调函数相关的唯一标识符。