跳到内容
Tauri

@tauri-apps/plugin-store

LazyStore

一个由后端层持久化的懒加载键值存储。

实现

  • IStore

构造函数

new LazyStore()
new LazyStore(path, options?): LazyStore

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

参数
参数类型描述
pathstringapp_data_dir 中保存存储的路径
options?StoreOptions存储配置选项
返回值

LazyStore

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

方法

clear()
clear(): Promise<void>

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

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

返回值

Promise<void>

的实现

IStore.clear

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

close()
close(): Promise<void>

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

返回值

Promise<void>

的实现

IStore.close

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

delete()
delete(key): Promise<boolean>

从存储中删除键值对。

参数
参数类型描述
keystring
返回值

Promise<boolean>

的实现

IStore.delete

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

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

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

类型参数
类型参数
T
返回值

Promise<[string, T][]>

的实现

IStore.entries

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

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

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

类型参数
类型参数
T
参数
参数类型描述
keystring
返回值

Promise<undefined | T>

的实现

IStore.get

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

has()
has(key): Promise<boolean>

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

参数
参数类型描述
keystring
返回值

Promise<boolean>

的实现

IStore.has

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

init()
init(): Promise<void>

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

返回值

Promise<void>

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

keys()
keys(): Promise<string[]>

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

返回值

Promise<string[]>

的实现

IStore.keys

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

length()
length(): Promise<number>

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

返回值

Promise<number>

的实现

IStore.length

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

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

监听存储上的更改。

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

Promise<UnlistenFn>

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

Since

2.0.0

的实现

IStore.onChange

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

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

监听存储键上的更改。

类型参数
类型参数
T
参数
参数类型描述
keystring
cb(value) => void
返回值

Promise<UnlistenFn>

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

Since

2.0.0

的实现

IStore.onKeyChange

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

reload()
reload(): Promise<void>

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

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

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

返回值

Promise<void>

的实现

IStore.reload

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

reset()
reset(): Promise<void>

将存储重置为其 default 值。

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

返回值

Promise<void>

的实现

IStore.reset

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

save()
save(): Promise<void>

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

返回值

Promise<void>

的实现

IStore.save

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

set()
set(key, value): Promise<void>

将键值对插入存储。

参数
参数类型描述
keystring
valueunknown
返回值

Promise<void>

的实现

IStore.set

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

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

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

类型参数
类型参数
T
返回值

Promise<T[]>

的实现

IStore.values

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


Store

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

继承自

  • 资源

实现

  • IStore

访问器

rid
获取签名
get rid(): number
返回值

number

继承自

Resource.rid

Source: undefined

方法

clear()
clear(): Promise<void>

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

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

返回值

Promise<void>

的实现

IStore.clear

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

close()
close(): Promise<void>

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

返回值

Promise<void>

的实现

IStore.close

继承自

Resource.close

Source: undefined

delete()
delete(key): Promise<boolean>

从存储中删除键值对。

参数
参数类型描述
keystring
返回值

Promise<boolean>

的实现

IStore.delete

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

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

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

类型参数
类型参数
T
返回值

Promise<[string, T][]>

的实现

IStore.entries

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

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

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

类型参数
类型参数
T
参数
参数类型描述
keystring
返回值

Promise<undefined | T>

的实现

IStore.get

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

has()
has(key): Promise<boolean>

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

参数
参数类型描述
keystring
返回值

Promise<boolean>

的实现

IStore.has

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

keys()
keys(): Promise<string[]>

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

返回值

Promise<string[]>

的实现

IStore.keys

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

length()
length(): Promise<number>

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

返回值

Promise<number>

的实现

IStore.length

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

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

监听存储上的更改。

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

Promise<UnlistenFn>

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

Since

2.0.0

的实现

IStore.onChange

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

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

监听存储键上的更改。

类型参数
类型参数
T
参数
参数类型描述
keystring
cb(value) => void
返回值

Promise<UnlistenFn>

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

Since

2.0.0

的实现

IStore.onKeyChange

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

reload()
reload(): Promise<void>

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

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

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

返回值

Promise<void>

的实现

IStore.reload

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

reset()
reset(): Promise<void>

将存储重置为其 default 值。

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

返回值

Promise<void>

的实现

IStore.reset

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

save()
save(): Promise<void>

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

返回值

Promise<void>

的实现

IStore.save

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

set()
set(key, value): Promise<void>

将键值对插入存储。

参数
参数类型描述
keystring
valueunknown
返回值

Promise<void>

的实现

IStore.set

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

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

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

类型参数
类型参数
T
返回值

Promise<T[]>

的实现

IStore.values

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

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

获取已加载的存储。

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

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

参数
参数类型描述
pathstring存储的路径。
返回值

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');
}

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

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

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

参数
参数类型描述
pathstringapp_data_dir 中保存存储的路径
options?StoreOptions存储配置选项
返回值

Promise<Store>

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

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

类型别名

StoreOptions

type StoreOptions: object;

用于创建存储的选项

类型声明

名称类型描述定义于
autoSave?boolean | number在修改时自动保存,并带有去抖动持续时间(毫秒),默认为 100 毫秒,传入 false 以禁用它Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L24
createNew?boolean强制创建一个具有默认值的新存储,即使它已经存在。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L36
deserializeFnName?string在 Rust 端插件构建器中注册的反序列化函数名称Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L32
serializeFnName?string在 Rust 端插件构建器中注册的序列化函数名称Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L28

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

函数

getStore()

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

获取已加载的存储。

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

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

参数

参数类型描述
pathstring存储的路径。

返回值

Promise<Store | null>

示例

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

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


load()

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

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

参数

参数类型描述
pathstringapp_data_dir 中保存存储的路径
options?StoreOptions存储配置选项

返回值

Promise<Store>

示例

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

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


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