tauri
调用您的自定义命令。
当 tauri.conf.json
中的 build.withGlobalTauri
设置为 true 时,该包也可以通过 window.__TAURI__.tauri
访问。
函数
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();
Since: 1.0.0
参数
名称 |
类型 |
默认 |
描述 |
|
|
|
文件的路径。 |
|
|
|
使用的协议。默认为 |
返回: 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' });
Since: 1.0.0
参数
名称 |
类型 |
描述 |
|
|
命令的名称。 |
|
传递给命令的参数(可选)。 |
返回: Promise<T>
一个Promise,resolving或rejecting来自后端的响应。