跳转到内容
Tauri

@tauri-apps/plugin-store

LazyStore

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

实现

  • IStore

构造函数

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

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

参数
参数类型描述
pathstring存储在 app_data_dir 路径的存储位置
options?StoreOptions存储配置选项
返回值

LazyStore

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

方法

clear()
clear(): Promise<void>

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

注意:若要清除存储并将其重置为其 默认 值,请使用 reset

返回值

Promise<void>

实现

IStore.clear

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

close()
close(): Promise<void>

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

返回值

Promise<void>

实现

IStore.close

来源: 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

来源: 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

来源: 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

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

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

如果 store 中存在给定的 key,则返回 true

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

Promise<boolean>

实现

IStore.has

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

init()
init(): Promise<void>

如果 store 没有加载,则初始化/加载 store。

返回值

Promise<void>

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

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

返回 store 中所有键的列表。

返回值

Promise<string[]>

实现

IStore.keys

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

length()
length(): Promise<number>

返回 store 中的键值对数量。

返回值

Promise<number>

实现

IStore.length

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

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

监听 store 的更改。

类型参数
类型参数
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()
onKeyChange<T>(key, cb): Promise<UnlistenFn>

监听 store 键的更改。

类型参数
类型参数
T
参数
参数类型描述
keystring
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()
reload(): Promise<void>

尝试将 store 的磁盘状态加载到内存中。

此方法适用于磁盘上的状态被用户编辑,并且您想同步更改的情况。

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

返回值

Promise<void>

实现

IStore.reload

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

reset()
reset(): Promise<void>

将 store 重置为其默认值。

如果没有设置默认值,此行为与 clear 相同。

返回值

Promise<void>

实现

IStore.reset

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

save()
save(): Promise<void>

将 store 保存到磁盘上的指定位置。

返回值

Promise<void>

实现

IStore.save

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

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

将键值对插入到 store 中。

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

Promise<void>

实现

IStore.set

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

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

返回 store 中所有值的列表。

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

Promise<T[]>

实现

IStore.values

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


存储

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

扩展

  • 资源

实现

  • IStore

访问器

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

数字

资源.rid 继承

来源: undefined

方法

clear()
clear(): Promise<void>

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

注意:要清除存储并重置为默认值,请使用 reset

返回值

Promise<void>

实现

IStore.clear

来源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

来源: undefined

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

从存储中删除一个键值对。

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

Promise<boolean>

实现

IStore.delete

来源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

来源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

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

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

如果 store 中存在给定的 key,则返回 true

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

Promise<boolean>

实现

IStore.has

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

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

返回 store 中所有键的列表。

返回值

Promise<string[]>

实现

IStore.keys

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

length()
length(): Promise<number>

返回 store 中的键值对数量。

返回值

Promise<number>

实现

IStore.length

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

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

监听 store 的更改。

类型参数
类型参数
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()
onKeyChange<T>(key, cb): Promise<UnlistenFn>

监听 store 键的更改。

类型参数
类型参数
T
参数
参数类型描述
keystring
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()
reload(): Promise<void>

尝试将 store 的磁盘状态加载到内存中。

此方法适用于磁盘上的状态被用户编辑,并且您想同步更改的情况。

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

返回值

Promise<void>

实现

IStore.reload

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

reset()
reset(): Promise<void>

将 store 重置为其默认值。

如果没有设置默认值,该函数的行为与 clear 相同。

返回值

Promise<void>

实现

IStore.reset

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

save()
save(): Promise<void>

将 store 保存到磁盘上的指定位置。

返回值

Promise<void>

实现

IStore.save

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

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

将键值对插入到 store 中。

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

Promise<void>

实现

IStore.set

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

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

返回 store 中所有值的列表。

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

Promise<T[]>

实现

IStore.values

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

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

获取已加载的store。

如果store未加载,返回 null。在这种情况下,你必须 加载 它。

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

参数
参数类型描述
pathstringstore的路径。
返回值

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

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

通过路径创建新的Store或加载现有的store。

参数
参数类型描述
pathstring存储在 app_data_dir 路径的存储位置
options?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

类型别名

StoreOptions

type StoreOptions: object;

创建store的选项

类型声明

名称类型描述定义在
autoSave?boolean | number自动在修改时保存,具有防抖时间(单位:毫秒),默认为100毫秒,传递 false 以禁用它。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L24
createNew?boolean强制创建一个新的store,即使它已经存在,也要使用默认值。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L36
deserializeFnName?string注册在 Rust 端插件构建器中的 deserialize 函数的名称来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/store/guest-js/index.ts#L32
serializeFnName?string注册在 Rust 端插件构建器中的 serialize 函数的名称来源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

函数

getStore()

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

获取已加载的store。

如果store未加载,返回 null。在这种情况下,你必须 加载 它。

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

参数

参数类型描述
pathstringstore的路径。

返回值

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


load()

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

通过路径创建新的Store或加载现有的store。

参数

参数类型描述
pathstring存储在 app_data_dir 路径的存储位置
options?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