26 Store

原文链接: https://tauri.app/plugin/store/

提供持久化的键值存储,可把状态保存到文件并按需加载。

支持的平台

平台支持程度说明
Windows完整支持
Linux完整支持
macOS完整支持
Android完整支持
iOS完整支持

设置

自动

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

1
npm run tauri add store
1
yarn run tauri add store
1
pnpm tauri add store
1
deno task tauri add store
1
bun tauri add store
1
cargo tauri add store

手动

  1. 在 src-tauri 文件夹中运行以下命令,把插件加入 Cargo.toml 里的项目依赖:

    1
    
    cargo add tauri-plugin-store
    
  2. 修改 lib.rs 初始化插件:

    1
    2
    3
    4
    5
    6
    7
    
    #[cfg_attr(mobile, tauri::mobile_entry_point)]
    pub fn run() {
        tauri::Builder::default()
            .plugin(tauri_plugin_store::init())
            .run(tauri::generate_context!())
            .expect("error while running tauri application");
    }
    
  3. 用你偏好的 JavaScript 包管理器安装 JavaScript 端绑定:

1
npm install @tauri-apps/plugin-store
1
yarn add @tauri-apps/plugin-store
1
pnpm add @tauri-apps/plugin-store
1
deno add npm:@tauri-apps/plugin-store
1
bun add @tauri-apps/plugin-store

用法

语言

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

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

1
2
3
import { LazyStore } from '@tauri-apps/plugin-store';

const store = new LazyStore('settings.json');

从 v1 以及 v2 beta/rc 迁移

语言

1
2
- import { Store } from '@tauri-apps/plugin-store';
+ import { LazyStore } from '@tauri-apps/plugin-store';
1
2
3
4
5
6
- 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 配置中修改权限才能启用它们。

更多信息请参阅能力概述,以及使用插件权限的分步指南。

1
2
3
4
5
6
{
  "permissions": [
    ...,
    "store:default"
  ]
}
最后修改 September 26, 2026: 更新 (630b11f59)