存储 (Store)
此插件提供了一个持久化的键值存储。这是处理应用程序状态的众多选项之一。有关其他选项的更多信息,请参阅状态管理概述。
此存储允许你将状态持久化到文件中,该文件可以按需保存和加载,包括在应用程序重启之间。请注意,此过程是异步的,因此需要在代码中进行相应处理。它既可以在 webview 中使用,也可以在 Rust 中使用。
此插件需要 Rust 版本至少为 **1.77.2**
| 平台 | 级别 | 备注 |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
安装存储插件即可开始使用。
使用你的项目包管理器添加依赖项
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 访客绑定
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';// when using `"withGlobalTauri": true`, you may use// const { load } = window.__TAURI__.store;
// Create a new store or load the existing one,// note that the options will be ignored if a `Store` with that path has already been createdconst store = await load('store.json', { autoSave: false });
// Set a value.await store.set('some-key', { value: 5 });
// Get a value.const val = await store.get<{ value: number }>('some-key');console.log(val); // { value: 5 }
// You can manually save the store after making changes.// Otherwise, it will save upon graceful exit// And if you set `autoSave` to a number or left empty,// it will save the changes to disk after a debounce delay, 100ms by default.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| { // Create a new store or load the existing one // this also put the store in the app's resource table // so your following `store` calls (from both Rust and JS) // will reuse the same store.
let store = app.store("store.json")?;
// Note that values must be serde_json::Value instances, // otherwise, they will not be compatible with the JavaScript bindings. store.set("some-key", json!({ "value": 5 }));
// Get a value from the store. let value = store.get("some-key").expect("Failed to get value from store"); println!("{}", value); // {"value":5}
// Remove the store from the resource table store.close_resource();
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}此外还有一个高级 JavaScript API LazyStore,它仅在首次访问时加载存储。
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';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", ]}默认权限
此权限集配置了存储插件可用的操作类型。
已授予权限
默认情况下,所有操作均已启用。
此默认权限集包括以下内容
允许加载 (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)允许保存
权限表
| 标识符 | 描述 |
|---|---|
|
|
在没有预先配置作用域的情况下启用 clear 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 clear 命令。 |
|
|
在没有预先配置作用域的情况下启用 delete 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 delete 命令。 |
|
|
在没有预先配置作用域的情况下启用 entries 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 entries 命令。 |
|
|
在没有预先配置作用域的情况下启用 get 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 get 命令。 |
|
|
在没有预先配置作用域的情况下启用 get_store 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 get_store 命令。 |
|
|
在没有预先配置作用域的情况下启用 has 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 has 命令。 |
|
|
在没有预先配置作用域的情况下启用 keys 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 keys 命令。 |
|
|
在没有预先配置作用域的情况下启用 length 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 length 命令。 |
|
|
在没有预先配置作用域的情况下启用 load 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 load 命令。 |
|
|
在没有预先配置作用域的情况下启用 reload 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 reload 命令。 |
|
|
在没有预先配置作用域的情况下启用 reset 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 reset 命令。 |
|
|
启用保存命令,不带任何预配置范围。 |
|
|
禁用保存命令,不带任何预配置范围。 |
|
|
在没有预先配置作用域的情况下启用 set 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 set 命令。 |
|
|
在没有预先配置作用域的情况下启用 values 命令。 |
|
|
在没有预先配置作用域的情况下拒绝 values 命令。 |
© 2026 Tauri 贡献者。CC-BY / MIT