跳转到内容

Store

GitHubnpmcrates.io
API Reference

这个插件提供了一个持久化的键值存储。这是在应用程序中处理状态的众多选项之一。更多信息请参见状态管理概览

该存储允许你将状态持久化到文件中,可以在应用重启之间按需保存和加载。请注意,这个过程是异步的,需要在代码中进行处理。它可以在 Webview 或 Rust 中使用。

This plugin requires a Rust version of at least 1.77.2

PlatformLevelNotes
windows
linux
macos
android
ios

安装 store 插件即可开始。

使用项目的包管理器添加依赖:

npm run tauri add store
import { load } from '@tauri-apps/plugin-store';
// 当使用 `"withGlobalTauri": true` 时,你可以使用
// const { load } = window.__TAURI__.store;
// 创建一个新 store 或加载现有的 store,
// 注意,如果已经创建了该路径的 Store,则选项将被忽略
const store = await load('store.json', { autoSave: false });
// 设置一个值。
await store.set('some-key', { value: 5 });
// 获取一个值。
const val = await store.get<{ value: number }>('some-key');
console.log(val); // { value: 5 }
// 你可以在更改后手动保存 store。
// 否则,它会在正常退出时保存。
// 如果你将 `autoSave` 设置为一个数字或不填,
// 它将在防抖延迟后将更改保存到磁盘,默认为 100ms。
await store.save();

还有一个高级 JavaScript API LazyStore,它只在首次访问时才加载 store。

import { LazyStore } from '@tauri-apps/plugin-store';
const store = new LazyStore('settings.json');
import { Store } from '@tauri-apps/plugin-store';
import { LazyStore } from '@tauri-apps/plugin-store';

默认情况下,所有潜在危险的插件命令和作用域都被阻止,无法访问。你必须在 capabilities 配置中修改权限才能启用它们。

有关更多信息,请参见功能概述以及使用插件权限的分步指南

src-tauri/capabilities/default.json
{
"permissions": [
...,
"store:default",
]
}

Default Permission

This permission set configures what kind of operations are available from the store plugin.

Granted Permissions

All operations are enabled by default.

This default permission set includes the following:

  • allow-load
  • allow-get-store
  • allow-set
  • allow-get
  • allow-has
  • allow-delete
  • allow-clear
  • allow-reset
  • allow-keys
  • allow-values
  • allow-entries
  • allow-length
  • allow-reload
  • allow-save

Permission Table

Identifier Description

store:allow-clear

Enables the clear command without any pre-configured scope.

store:deny-clear

Denies the clear command without any pre-configured scope.

store:allow-delete

Enables the delete command without any pre-configured scope.

store:deny-delete

Denies the delete command without any pre-configured scope.

store:allow-entries

Enables the entries command without any pre-configured scope.

store:deny-entries

Denies the entries command without any pre-configured scope.

store:allow-get

Enables the get command without any pre-configured scope.

store:deny-get

Denies the get command without any pre-configured scope.

store:allow-get-store

Enables the get_store command without any pre-configured scope.

store:deny-get-store

Denies the get_store command without any pre-configured scope.

store:allow-has

Enables the has command without any pre-configured scope.

store:deny-has

Denies the has command without any pre-configured scope.

store:allow-keys

Enables the keys command without any pre-configured scope.

store:deny-keys

Denies the keys command without any pre-configured scope.

store:allow-length

Enables the length command without any pre-configured scope.

store:deny-length

Denies the length command without any pre-configured scope.

store:allow-load

Enables the load command without any pre-configured scope.

store:deny-load

Denies the load command without any pre-configured scope.

store:allow-reload

Enables the reload command without any pre-configured scope.

store:deny-reload

Denies the reload command without any pre-configured scope.

store:allow-reset

Enables the reset command without any pre-configured scope.

store:deny-reset

Denies the reset command without any pre-configured scope.

store:allow-save

Enables the save command without any pre-configured scope.

store:deny-save

Denies the save command without any pre-configured scope.

store:allow-set

Enables the set command without any pre-configured scope.

store:deny-set

Denies the set command without any pre-configured scope.

store:allow-values

Enables the values command without any pre-configured scope.

store:deny-values

Denies the values command without any pre-configured scope.


© 2026 Tauri Contributors. CC-BY / MIT