跳到主要內容

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 陣列中定義其存取範圍。

範例

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字串未定義檔案路徑。
protocol字串'asset'要使用的通訊協定。預設為 asset。只有在使用自訂通訊協定時才需要設定此值。

傳回:字串

可以在 webview 中用作來源的 URL。

invoke

invoke<T>(cmd: 字串, args?: InvokeArgs): Promise<T>

傳送訊息至後端。

範例

import { invoke } from '@tauri-apps/api/tauri';
await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });

: 1.0.0

類型參數

  • T

參數

名稱類型說明
cmd字串命令名稱。
argsInvokeArgs傳遞給命令的選用引數。

傳回:Promise<T>

解析或拒絕後端回應的承諾。

transformCallback

transformCallback(callback?: fn, once?: 布林值): 數字

將回呼函式轉換為字串識別碼,此識別碼可以傳遞至後端。後端使用此識別碼來 eval() 回呼函式。

: 1.0.0

參數

名稱類型預設值
callback?(response: 任何) => 未定義
once布林值

傳回:數字

與回呼函數關聯的唯一識別碼。