Store
这个插件提供了一个持久化的键值存储。这是在应用程序中处理状态的众多选项之一。更多信息请参见状态管理概览。
该存储允许你将状态持久化到文件中,可以在应用重启之间按需保存和加载。请注意,这个过程是异步的,需要在代码中进行处理。它可以在 Webview 或 Rust 中使用。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
安装 store 插件即可开始。
使用项目的包管理器添加依赖:
npm run tauri add storeyarn run tauri add storepnpm tauri add storedeno task tauri add storebun tauri add storecargo tauri add store-
在
src-tauri文件夹中运行以下命令,将插件添加到Cargo.toml的依赖中:cargo add tauri-plugin-store -
修改
lib.rs以初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_store::Builder::new().build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你偏好的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-storeyarn add @tauri-apps/plugin-storepnpm add @tauri-apps/plugin-storedeno add npm:@tauri-apps/plugin-storebun add @tauri-apps/plugin-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();use tauri::Wry;use tauri_plugin_store::StoreExt;use serde_json::json;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { // 创建一个新的 store 或加载现有的 store // 这也会将 store 放入应用的资源表中 // 因此你后续的 `store` 调用(来自 Rust 和 JS) // 将复用同一个 store。
let store = app.store("store.json")?;
// 注意,值必须是 serde_json::Value 的实例, // 否则,它们将与 JavaScript 绑定不兼容。 store.set("some-key", json!({ "value": 5 }));
// 从 store 中获取一个值。 let value = store.get("some-key").expect("Failed to get value from store"); println!("{}", value); // {"value":5}
// 从资源表中移除 store store.close_resource();
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}LazyStore
Section titled “LazyStore”还有一个高级 JavaScript API LazyStore,它只在首次访问时才加载 store。
import { LazyStore } from '@tauri-apps/plugin-store';
const store = new LazyStore('settings.json');从 v1 和 v2 beta/rc 迁移
Section titled “从 v1 和 v2 beta/rc 迁移”import { Store } from '@tauri-apps/plugin-store';import { LazyStore } from '@tauri-apps/plugin-store';with_store(app.handle().clone(), stores, path, |store| { store.insert("some-key".to_string(), json!({ "value": 5 }))?; Ok(())});let store = app.store(path)?;store.set("some-key".to_string(), json!({ "value": 5 }));默认情况下,所有潜在危险的插件命令和作用域都被阻止,无法访问。你必须在 capabilities 配置中修改权限才能启用它们。
有关更多信息,请参见功能概述以及使用插件权限的分步指南。
{ "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-loadallow-get-storeallow-setallow-getallow-hasallow-deleteallow-clearallow-resetallow-keysallow-valuesallow-entriesallow-lengthallow-reloadallow-save
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the clear command without any pre-configured scope. |
|
|
Denies the clear command without any pre-configured scope. |
|
|
Enables the delete command without any pre-configured scope. |
|
|
Denies the delete command without any pre-configured scope. |
|
|
Enables the entries command without any pre-configured scope. |
|
|
Denies the entries command without any pre-configured scope. |
|
|
Enables the get command without any pre-configured scope. |
|
|
Denies the get command without any pre-configured scope. |
|
|
Enables the get_store command without any pre-configured scope. |
|
|
Denies the get_store command without any pre-configured scope. |
|
|
Enables the has command without any pre-configured scope. |
|
|
Denies the has command without any pre-configured scope. |
|
|
Enables the keys command without any pre-configured scope. |
|
|
Denies the keys command without any pre-configured scope. |
|
|
Enables the length command without any pre-configured scope. |
|
|
Denies the length command without any pre-configured scope. |
|
|
Enables the load command without any pre-configured scope. |
|
|
Denies the load command without any pre-configured scope. |
|
|
Enables the reload command without any pre-configured scope. |
|
|
Denies the reload command without any pre-configured scope. |
|
|
Enables the reset command without any pre-configured scope. |
|
|
Denies the reset command without any pre-configured scope. |
|
|
Enables the save command without any pre-configured scope. |
|
|
Denies the save command without any pre-configured scope. |
|
|
Enables the set command without any pre-configured scope. |
|
|
Denies the set command without any pre-configured scope. |
|
|
Enables the values command without any pre-configured scope. |
|
|
Denies the values command without any pre-configured scope. |
© 2026 Tauri Contributors. CC-BY / MIT