存储
此插件提供持久的键值存储。这是处理应用程序状态的多种选项之一。有关更多信息,请参阅状态管理概述。
此存储将允许您将状态持久化到文件,可以在需要时保存和加载,包括在应用程序重启之间。请注意,此过程是异步的,需要在您的代码中处理。它可以在 webview 或 Rust 中使用。
支持的平台
此插件至少需要 Rust 版本 1.77.2
平台 | 级别 | 备注 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
设置
为了开始使用,请安装存储插件。
使用您的项目包管理器添加依赖项
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");}
LazyStore
还有一个高层次的 JavaScript API LazyStore
,它只在首次访问时加载存储
import { LazyStore } from '@tauri-apps/plugin-store';
const store = new LazyStore('settings.json');
从 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", ]}
默认权限
此权限集配置了从存储插件可用的操作类型。
授予的权限
默认情况下启用所有操作。
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
权限表
标识符 | 描述 |
---|---|
|
启用清除命令,无需任何预先配置的作用域。 |
|
拒绝清除命令,无需任何预先配置的作用域。 |
|
启用删除命令,无需任何预先配置的作用域。 |
|
拒绝删除命令,无需任何预先配置的作用域。 |
|
启用条目命令,无需任何预先配置的作用域。 |
|
拒绝条目命令,无需任何预先配置的作用域。 |
|
启用获取命令,无需任何预先配置的作用域。 |
|
拒绝获取命令,无需任何预先配置的作用域。 |
|
启用获取存储命令,无需任何预先配置的作用域。 |
|
拒绝获取存储命令,无需任何预先配置的作用域。 |
|
启用拥有命令,无需任何预先配置的作用域。 |
|
拒绝拥有命令,无需任何预先配置的作用域。 |
|
启用键命令,无需任何预先配置的作用域。 |
|
拒绝键命令,无需任何预先配置的作用域。 |
|
启用长度命令,无需任何预先配置的作用域。 |
|
拒绝长度命令,无需任何预先配置的作用域。 |
|
启用加载命令,无需任何预先配置的作用域。 |
|
拒绝加载命令,无需任何预先配置的作用域。 |
|
启用重新加载命令,无需任何预先配置的作用域。 |
|
拒绝重新加载命令,无需任何预先配置的作用域。 |
|
启用重置命令,无需任何预先配置的作用域。 |
|
拒绝重置命令,无需任何预先配置的作用域。 |
|
启用保存命令,无需任何预先配置的作用域。 |
|
拒绝保存命令,无需任何预先配置的作用域。 |
|
启用设置命令,无需任何预先配置的作用域。 |
|
拒绝设置命令,无需任何预先配置的作用域。 |
|
启用值命令,无需任何预先配置的作用域。 |
|
拒绝值命令,无需任何预先配置的作用域。 |
© 2025 Tauri 贡献者。CC-BY / MIT