core
调用您的自定义命令。
当 tauri.conf.json
中 app.withGlobalTauri
设置为 true
时,此包也可以通过 window.__TAURI__.core
访问。
类型参数 | 默认类型 |
---|---|
T | 未知 |
new Channel<T>(onmessage?): Channel<T>
参数 | 类型 |
---|---|
onmessage ? | (response ) => void |
Channel
<T
>
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L87
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
id | 数字 | 从 transformCallback 返回的回调 ID | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L79 |
get onmessage(): (response) => void
set onmessage(handler): void
参数 | 类型 |
---|---|
处理程序 | (response ) => void |
函数
参数 | 类型 |
---|---|
响应 | T |
空
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L142
__TAURI_TO_IPC_KEY__(): string
字符串
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L146
toJSON(): string
字符串
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L150
new PluginListener( plugin, event, channelId): PluginListener
参数 | 类型 |
---|---|
插件 | 字符串 |
event | 字符串 |
频道 ID | 数字 |
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L161
unregister(): Promise<void>
Promise
<void
>
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L167
通过 tauri::Manager::resources_table
API 存储的 Rust 后端资源。
该资源存在于主进程中,在 Javascript 世界中不存在,因此除非应用程序退出,否则不会自动清理。如果您想提前清理它,请调用 Resource.close
import { Resource, invoke } from '@tauri-apps/api/core';export class DatabaseHandle extends Resource { static async open(path: string): Promise<DatabaseHandle> { const rid: number = await invoke('open_db', { path }); return new DatabaseHandle(rid); }
async execute(sql: string): Promise<void> { await invoke('execute_sql', { rid: this.rid, sql }); }}
new Resource(rid): Resource
参数 | 类型 |
---|---|
rid | 数字 |
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L313
get rid(): number
数字
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L309
close(): Promise<void>
销毁并从内存中清理此资源。您不应再对此对象调用任何方法,并应放弃对它的任何引用。
Promise
<void
>
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L321
2.0.0
属性 | 类型 | 定义于 |
---|---|---|
headers | HeadersInit | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L224 |
type InvokeArgs: Record<string, unknown> | number[] | ArrayBuffer | Uint8Array;
命令参数。
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L218
type PermissionState: "granted" | "denied" | "prompt" | "prompt-with-rationale";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L193
const SERIALIZE_TO_IPC_FN: "__TAURI_TO_IPC_KEY__" = '__TAURI_TO_IPC_KEY__';
一个键,用于在您的类型上实现一个特殊函数,该函数定义了您的类型在跨 IPC 传递时应如何序列化。
给定一个 Rust 中的类型,如下所示
#[derive(serde::Serialize, serde::Deserialize)enum UserId { String(String), Number(u32),}
UserId::String("id")
将序列化为 { String: "id" }
,因此我们需要将相同的结构传递回 Rust
import { SERIALIZE_TO_IPC_FN } from "@tauri-apps/api/core"
class UserIdString { id constructor(id) { this.id = id }
[SERIALIZE_TO_IPC_FN]() { return { String: this.id } }}
class UserIdNumber { id constructor(id) { this.id = id }
[SERIALIZE_TO_IPC_FN]() { return { Number: this.id } }}
type UserId = UserIdString | UserIdNumber
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L59
function addPluginListener<T>( plugin, event,cb): Promise<PluginListener>
为插件事件添加监听器。
类型参数 |
---|
T |
参数 | 类型 |
---|---|
插件 | 字符串 |
event | 字符串 |
cb | (payload ) => void |
用于停止监听事件的监听器对象。
2.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L182
function checkPermissions<T>(plugin): Promise<T>
获取插件的权限状态。
插件作者应使用此功能来封装其实际实现。
类型参数 |
---|
T |
参数 | 类型 |
---|---|
插件 | 字符串 |
Promise
<T
>
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L200
function convertFileSrc(filePath, protocol): string
将设备文件路径转换为可由 webview 加载的 URL。请注意,asset:
和 http://asset.localhost
必须添加到 tauri.conf.json
中的 app.security.csp
中。例如,CSP 值:"csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost"
,用于在图像源上使用资产协议。
此外,"enable" : "true"
必须添加到 tauri.conf.json
中的 app.security.assetProtocol
中,并且其访问范围必须在同一 assetProtocol
对象的 scope
数组中定义。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
文件路径 | 字符串 | 未定义 | 文件路径。 |
协议 | 字符串 | 'asset' | 要使用的协议。默认为 asset 。您只在使用自定义协议时才需要设置此项。 |
字符串
可在 webview 中用作源的 URL。
import { appDataDir, join } from '@tauri-apps/api/path';import { convertFileSrc } from '@tauri-apps/api/core';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
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L280
function invoke<T>( cmd, args,options?): Promise<T>
向后端发送消息。
类型参数 |
---|
T |
参数 | 类型 | 描述 |
---|---|---|
cmd | 字符串 | 命令名称。 |
args | InvokeArgs | 传递给命令的可选参数。 |
选项 ? | InvokeOptions | 请求选项。 |
Promise
<T
>
一个解析或拒绝后端响应的 Promise。
import { invoke } from '@tauri-apps/api/core';await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L242
function isTauri(): boolean
布尔值 (boolean)
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L328
function requestPermissions<T>(plugin): Promise<T>
请求权限。
插件作者应使用此功能来封装其实际实现。
类型参数 |
---|
T |
参数 | 类型 |
---|---|
插件 | 字符串 |
Promise
<T
>
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L209
function transformCallback<T>(callback?, once?): number
将回调存储在已知位置,并返回可传递给后端的标识符。后端使用该标识符 eval()
回调。
类型参数 | 默认类型 |
---|---|
T | 未知 |
参数 | 类型 | 默认值 |
---|---|---|
回调 ? | (response ) => void | 未定义 |
一次 ? | 布尔值 (boolean) | false |
数字
与回调函数关联的唯一标识符。
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L69
© 2025 Tauri 贡献者。CC-BY / MIT