跳到内容
Tauri

core

调用您的自定义命令。

app.withGlobalTauritauri.conf.json 中设置为 true 时,此包也可以通过 window.__TAURI__.core 访问。

Channel<T>

类型参数

类型参数默认类型
Tunknown

构造函数

new Channel()
new Channel<T>(): Channel<T>
返回值

Channel<T>

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L88

属性

属性类型定义在
idnumber来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L78

访问器

onmessage
Get Signature
get onmessage(): (response) => void
返回值

Function

参数
Parameter类型
responseT
返回值

void

Set Signature
set onmessage(handler): void
参数
Parameter类型
handler(response) => void
返回值

void

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L118

方法

__TAURI_TO_IPC_KEY__()
__TAURI_TO_IPC_KEY__(): string
返回值

string

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L122

toJSON()
toJSON(): string
返回值

string

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L126


PluginListener

构造函数

new PluginListener()
new PluginListener(
plugin,
event,
channelId): PluginListener
参数
Parameter类型
pluginstring
eventstring
channelIdnumber
返回值

PluginListener

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L137

属性

属性类型定义在
channelIdnumber来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L135
eventstring来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L134
pluginstring来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L133

方法

unregister()
unregister(): Promise<void>
返回值

Promise<void>

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L143


Resource

通过 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()
new Resource(rid): Resource
参数
Parameter类型
ridnumber
返回值

Resource

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L290

访问器

rid
Get Signature
get rid(): number
返回值

number

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L286

方法

close()
close(): Promise<void>

销毁并从内存中清理此资源。 您不应再在此对象上调用任何方法,并且应删除对其的任何引用。

返回值

Promise<void>

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L298

接口

InvokeOptions

2.0.0

属性

属性类型定义在
headersRecord<string, string> | Headers来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L201

类型别名

InvokeArgs

type InvokeArgs: Record<string, unknown> | number[] | ArrayBuffer | Uint8Array;

命令参数。

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L195


PermissionState

type PermissionState: "granted" | "denied" | "prompt" | "prompt-with-rationale";

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L170

变量

SERIALIZE_TO_IPC_FN

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#L60

函数

addPluginListener()

function addPluginListener<T>(
plugin,
event,
cb): Promise<PluginListener>

向插件事件添加监听器。

类型参数

类型参数
T

参数

Parameter类型
pluginstring
eventstring
cb(payload) => void

返回值

Promise<PluginListener>

用于停止监听事件的监听器对象。

2.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L158


checkPermissions()

function checkPermissions<T>(plugin): Promise<T>

获取插件的权限状态。

插件作者应使用此方法来包装其实际实现。

类型参数

类型参数
T

参数

Parameter类型
pluginstring

返回值

Promise<T>

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L177


convertFileSrc()

function convertFileSrc(filePath, protocol): string

将设备文件路径转换为可由 webview 加载的 URL。 请注意,asset:http://asset.localhost 必须添加到 app.security.csp in tauri.conf.json 中。 示例 CSP 值:"csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost" 以在图像源上使用 asset 协议。

此外,"enable" : "true" 必须添加到 app.security.assetProtocol in tauri.conf.json,并且其访问作用域必须在同一 assetProtocol 对象上的 scope 数组中定义。

参数

Parameter类型默认值描述
filePathstringundefined文件路径。
protocolstring'asset'要使用的协议。 默认为 asset。 仅在使用自定义协议时才需要设置此项。

返回值

string

可用作 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#L257


invoke()

function invoke<T>(
cmd,
args,
options?): Promise<T>

向后端发送消息。

类型参数

类型参数
T

参数

Parameter类型描述
cmdstring命令名称。
argsInvokeArgs要传递给命令的可选参数。
options?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#L219


isTauri()

function isTauri(): boolean

返回值

boolean

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L305


requestPermissions()

function requestPermissions<T>(plugin): Promise<T>

请求权限。

插件作者应使用此方法来包装其实际实现。

类型参数

类型参数
T

参数

Parameter类型
pluginstring

返回值

Promise<T>

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L186


transformCallback()

function transformCallback<T>(callback?, once?): number

将回调函数转换为可以传递到后端的字符串标识符。 后端使用该标识符来 eval() 回调。

类型参数

类型参数默认类型
Tunknown

参数

Parameter类型默认值
callback?(response) => voidundefined
once?booleanfalse

返回值

number

与回调函数关联的唯一标识符。

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L70


© 2025 Tauri 贡献者。 CC-BY / MIT