跳到内容
Tauri

@tauri-apps/plugin-store

由后端层持久化的延迟加载键值存储。

  • IStore

new LazyStore(path, options?): LazyStore

请注意,如果其他人已经创建了存储,则不应用选项

参数类型描述
path字符串存储的路径在 app_data_dir
选项?StoreOptions存储配置选项

LazyStore

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L96

clear(): Promise<void>

清除存储,移除所有键值对。

注意:要清除存储并将其重置为其 default 值,请改用 reset

Promise<void>

IStore.clear

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L124

close(): Promise<void>

关闭存储并从内存中清除此资源。你不应再对此对象调用任何方法,并应放弃对它的任何引用。

Promise<void>

IStore.close

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L169

delete(key): Promise<boolean>

从存储中移除键值对。

参数类型描述
字符串

Promise<boolean>

IStore.delete

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L120

entries<T>(): Promise<[string, T][]>

返回存储中所有条目的列表。

类型参数
T

Promise<[string, T][]>

IStore.entries

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L140

get<T>(key): Promise<undefined | T>

返回给定 key 的值,如果键不存在则返回 undefined

类型参数
T
参数类型描述
字符串

Promise<undefined | T>

IStore.get

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L112

has(key): Promise<boolean>

如果给定 key 存在于存储中,则返回 true

参数类型描述
字符串

Promise<boolean>

IStore.has

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L116

init(): Promise<void>

如果存储尚未加载,则初始化/加载存储

Promise<void>

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L104

keys(): Promise<string[]>

返回存储中所有键的列表。

Promise<string[]>

IStore.keys

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L132

length(): Promise<number>

返回存储中键值对的数量。

Promise<number>

IStore.length

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L144

onChange<T>(cb): Promise<UnlistenFn>

监听存储中的更改。

类型参数
T
参数类型描述
cb(key, value) => void

Promise<UnlistenFn>

一个解析为取消监听事件的函数的 Promise。

2.0.0

IStore.onChange

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L163

onKeyChange<T>(key, cb): Promise<UnlistenFn>

监听存储键上的更改。

类型参数
T
参数类型描述
字符串
cb(value) => void

Promise<UnlistenFn>

一个解析为取消监听事件的函数的 Promise。

2.0.0

IStore.onKeyChange

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L156

reload(): Promise<void>

尝试将存储 path 处的磁盘状态加载到内存中。

如果磁盘状态由用户编辑,并且您想同步更改,此方法很有用。

注意:此方法不发出更改事件。

Promise<void>

IStore.reload

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L148

reset(): Promise<void>

将存储重置为其 default 值。

如果未设置默认值,此方法的行为与 clear 相同。

Promise<void>

IStore.reset

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L128

save(): Promise<void>

将存储保存到存储 path 处的磁盘。

Promise<void>

IStore.save

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L152

set(key, value): Promise<void>

将键值对插入到存储中。

参数类型描述
字符串
未知

Promise<void>

IStore.set

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L108

values<T>(): Promise<T[]>

返回存储中所有值的列表。

类型参数
T

Promise<T[]>

IStore.values

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L136


由后端层持久化的键值存储。

  • 资源
  • IStore

get rid(): number

数字

资源.rid

来源: 未定义

clear(): Promise<void>

清除存储,移除所有键值对。

注意:要清除存储并将其重置为其 default 值,请改用 reset

Promise<void>

IStore.clear

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L259

close(): Promise<void>

销毁并从内存中清理此资源。您不应再对此对象调用任何方法,并应放弃对它的任何引用。

Promise<void>

IStore.close

资源.close

来源: 未定义

delete(key): Promise<boolean>

从存储中移除键值对。

参数类型描述
字符串

Promise<boolean>

IStore.delete

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L252

entries<T>(): Promise<[string, T][]>

返回存储中所有条目的列表。

类型参数
T

Promise<[string, T][]>

IStore.entries

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L275

get<T>(key): Promise<undefined | T>

返回给定 key 的值,如果键不存在则返回 undefined

类型参数
T
参数类型描述
字符串

Promise<undefined | T>

IStore.get

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L237

has(key): Promise<boolean>

如果给定 key 存在于存储中,则返回 true

参数类型描述
字符串

Promise<boolean>

IStore.has

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L245

keys(): Promise<string[]>

返回存储中所有键的列表。

Promise<string[]>

IStore.keys

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L267

length(): Promise<number>

返回存储中键值对的数量。

Promise<number>

IStore.length

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L279

onChange<T>(cb): Promise<UnlistenFn>

监听存储中的更改。

类型参数
T
参数类型描述
cb(key, value) => void

Promise<UnlistenFn>

一个解析为取消监听事件的函数的 Promise。

2.0.0

IStore.onChange

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L302

onKeyChange<T>(key, cb): Promise<UnlistenFn>

监听存储键上的更改。

类型参数
T
参数类型描述
字符串
cb(value) => void

Promise<UnlistenFn>

一个解析为取消监听事件的函数的 Promise。

2.0.0

IStore.onKeyChange

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L291

reload(): Promise<void>

尝试将存储 path 处的磁盘状态加载到内存中。

如果磁盘状态由用户编辑,并且您想同步更改,此方法很有用。

注意:此方法不发出更改事件。

Promise<void>

IStore.reload

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L283

reset(): Promise<void>

将存储重置为其 default 值。

如果未设置默认值,此方法的行为与 clear 相同。

Promise<void>

IStore.reset

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L263

save(): Promise<void>

将存储保存到存储 path 处的磁盘。

Promise<void>

IStore.save

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L287

set(key, value): Promise<void>

将键值对插入到存储中。

参数类型描述
字符串
未知

Promise<void>

IStore.set

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L229

values<T>(): Promise<T[]>

返回存储中所有值的列表。

类型参数
T

Promise<T[]>

IStore.values

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L271

static get(path): Promise<null | Store>

获取一个已加载的存储。

如果存储未加载,则返回 null。在这种情况下,您必须 加载 它。

当您已经知道存储已加载并且只需要访问其实例时,此函数更有用。否则请首选 Store.load

参数类型描述
path字符串存储的路径。

Promise<null | Store>

import { Store } from '@tauri-apps/api/store';
let store = await Store.get('store.json');
if (!store) {
store = await Store.load('store.json');
}

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L223

static load(path, options?): Promise<Store>

创建新的存储或加载具有路径的现有存储。

参数类型描述
path字符串存储的路径在 app_data_dir
选项?StoreOptions存储配置选项

Promise<Store>

import { Store } from '@tauri-apps/api/store';
const store = await Store.load('store.json');

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L196

type StoreOptions: object;

创建存储的选项

名称类型描述定义于
自动保存boolean | number修改时自动保存并带延迟时间(毫秒),默认是 100 毫秒,传入 false 禁用来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L24
创建新布尔值 (boolean)强制创建新存储并带有默认值,即使它已经存在。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L36
反序列化函数名字符串在 Rust 侧插件构建器中注册的反序列化函数的名称来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L32
序列化函数名字符串在 Rust 侧插件构建器中注册的序列化函数的名称来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L28

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L20

function getStore(path): Promise<Store | null>

获取一个已加载的存储。

如果存储未加载,则返回 null。在这种情况下,您必须 加载 它。

当您已经知道存储已加载并且只需要访问其实例时,此函数更有用。否则请首选 Store.load

参数类型描述
path字符串存储的路径。

Promise<Store | null>

import { getStore } from '@tauri-apps/api/store';
const store = await getStore('store.json');

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L74


function load(path, options?): Promise<Store>

创建新的存储或加载具有路径的现有存储。

参数类型描述
path字符串存储的路径在 app_data_dir
选项?StoreOptions存储配置选项

Promise<Store>

import { Store } from '@tauri-apps/api/store';
const store = await Store.load('store.json');

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L51


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