Skip to content
Tauri 中文网

core

Invoke your custom commands.

This package is also accessible with window.__TAURI__.core when app.withGlobalTauri in tauri.conf.json is set to true.

Type ParameterDefault type
Tunknown

new Channel<T>(onmessage?): Channel<T>
ParameterType
onmessage?(response) => void

Channel<T>

Source: undefined

PropertyTypeDescriptionDefined in
idnumberThe callback id returned from transformCallbackSource: undefined

get onmessage(): (response) => void
set onmessage(handler): void
ParameterType
handler(response) => void

Function

ParameterType
responseT

void

Source: undefined

__TAURI_TO_IPC_KEY__(): string

string

Source: undefined

toJSON(): string

string

Source: undefined


new PluginListener(
plugin,
event,
channelId): PluginListener
ParameterType
pluginstring
eventstring
channelIdnumber

PluginListener

Source: undefined

PropertyTypeDefined in
channelIdnumberSource: undefined
eventstringSource: undefined
pluginstringSource: undefined

unregister(): Promise<void>

Promise<void>

Source: undefined


A rust-backed resource stored through tauri::Manager::resources_table API.

The resource lives in the main process and does not exist in the Javascript world, and thus will not be cleaned up automatiacally except on application exit. If you want to clean it up early, call 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
ParameterType
ridnumber

Resource

Source: undefined

get rid(): number

number

Source: undefined

close(): Promise<void>

Destroys and cleans up this resource from memory. You should not call any method on this object anymore and should drop any reference to it.

Promise<void>

Source: undefined

2.0.0

PropertyTypeDefined in
headersHeadersInitSource: undefined

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

Command arguments.

1.0.0

Source: undefined


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

Source: undefined

const SERIALIZE_TO_IPC_FN: "__TAURI_TO_IPC_KEY__" = '__TAURI_TO_IPC_KEY__';

A key to be used to implement a special function on your types that define how your type should be serialized when passing across the IPC.

Given a type in Rust that looks like this

#[derive(serde::Serialize, serde::Deserialize)
enum UserId {
String(String),
Number(u32),
}

UserId::String("id") would be serialized into { String: "id" } and so we need to pass the same structure back to 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

Source: undefined

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

Adds a listener to a plugin event.

Type Parameter
T
ParameterType
pluginstring
eventstring
cb(payload) => void

Promise<PluginListener>

The listener object to stop listening to the events.

2.0.0

Source: undefined


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

Get permission state for a plugin.

This should be used by plugin authors to wrap their actual implementation.

Type Parameter
T
ParameterType
pluginstring

Promise<T>

Source: undefined


function convertFileSrc(filePath, protocol): string

Convert a device file path to an URL that can be loaded by the webview. Note that asset: and http://asset.localhost must be added to app.security.csp in tauri.conf.json. Example CSP value: "csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost" to use the asset protocol on image sources.

Additionally, "enable" : "true" must be added to app.security.assetProtocol in tauri.conf.json and its access scope must be defined on the scope array on the same assetProtocol object.

ParameterTypeDefault valueDescription
filePathstringundefinedThe file path.
protocolstring'asset'The protocol to use. Defaults to asset. You only need to set this when using a custom protocol.

string

the URL that can be used as source on the webview.

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

Source: undefined


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

Sends a message to the backend.

Type Parameter
T
ParameterTypeDescription
cmdstringThe command name.
argsInvokeArgsThe optional arguments to pass to the command.
options?InvokeOptionsThe request options.

Promise<T>

A promise resolving or rejecting to the backend response.

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

1.0.0

Source: undefined


function isTauri(): boolean

boolean

Source: undefined


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

Request permissions.

This should be used by plugin authors to wrap their actual implementation.

Type Parameter
T
ParameterType
pluginstring

Promise<T>

Source: undefined


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

Stores the callback in a known location, and returns an identifier that can be passed to the backend. The backend uses the identifier to eval() the callback.

Type ParameterDefault type
Tunknown
ParameterTypeDefault value
callback?(response) => voidundefined
once?booleanfalse

number

An unique identifier associated with the callback function.

1.0.0

Source: undefined


Tauri v2.8 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站