存储 (Store)
此插件提供了一个持久化的键值存储。这是您在应用程序中处理状态的众多选项之一。有关其他选项的更多信息,请参阅状态管理概述。
此存储将允许您将状态持久化到一个文件,该文件可以按需保存和加载,包括在应用程序重启之间。请注意,此过程是异步的,需要在您的代码中进行处理。它可以在 webview 或 Rust 中使用。
此插件需要 Rust 版本至少为 **1.77.2**
平台 | 级别 | 备注 |
---|---|---|
Windows | ||
Linux | ||
macOS | ||
Android | ||
iOS |
安装 store 插件以开始使用。
使用你的项目包管理器添加依赖项
npm run tauri add store
yarn run tauri add store
pnpm tauri add store
deno task tauri add store
bun tauri add store
cargo 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 calls `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", ]}
默认权限
此权限集配置了 store 插件可以进行的操作类型。
已授予权限
默认情况下,所有操作均已启用。
此默认权限集包括以下内容
允许加载
允许获取存储
允许设置
允许获取
允许存在
允许删除
允许清除
允许重置
允许键
允许值
允许条目
允许长度
允许重新加载
允许保存
权限表
标识符 | 描述 |
---|---|
|
启用清除命令,不带任何预配置范围。 |
|
拒绝清除命令,不带任何预配置范围。 |
|
启用删除命令,不带任何预配置范围。 |
|
拒绝删除命令,不带任何预配置范围。 |
|
启用 entries 命令,不带任何预配置范围。 |
|
拒绝 entries 命令,不带任何预配置范围。 |
|
启用 get 命令,不带任何预配置范围。 |
|
拒绝 get 命令,不带任何预配置范围。 |
|
启用 get_store 命令,不带任何预配置范围。 |
|
拒绝 get_store 命令,不带任何预配置范围。 |
|
启用 has 命令,不带任何预配置范围。 |
|
拒绝 has 命令,不带任何预配置范围。 |
|
启用 keys 命令,不带任何预配置范围。 |
|
拒绝 keys 命令,不带任何预配置范围。 |
|
启用 length 命令,不带任何预配置范围。 |
|
拒绝 length 命令,不带任何预配置范围。 |
|
启用 load 命令,不带任何预配置范围。 |
|
拒绝 load 命令,不带任何预配置范围。 |
|
启用 reload 命令,不带任何预配置范围。 |
|
拒绝 reload 命令,不带任何预配置范围。 |
|
启用 reset 命令,不带任何预配置范围。 |
|
拒绝 reset 命令,不带任何预配置范围。 |
|
启用保存命令,不带任何预配置范围。 |
|
禁用保存命令,不带任何预配置范围。 |
|
启用 set 命令,不带任何预配置范围。 |
|
拒绝 set 命令,不带任何预配置范围。 |
|
启用 values 命令,不带任何预配置范围。 |
|
拒绝 values 命令,不带任何预配置范围。 |
© 2025 Tauri 贡献者。CC-BY / MIT