Store
此插件提供持久的键值存储。这是在应用程序中处理状态的众多选项之一。有关其他选项的更多信息,请参阅状态管理概述。
此存储将允许您将状态持久化到文件中,该文件可以按需保存和加载,包括在应用程序重启之间。请注意,此过程是异步的,需要在您的代码中处理它。它可以在 webview 或 Rust 中使用。
支持的平台
此插件至少需要 1.77.2 的 Rust 版本
平台 | 级别 | 备注 |
---|---|---|
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 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';// 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", ]}
默认权限
此权限集配置了 store 插件中可用的操作类型。
已授予的权限
默认情况下启用所有操作。
此默认权限集包括以下内容
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
权限表
标识符 | 描述 |
---|---|
|
启用 clear 命令,无需任何预配置的作用域。 |
|
拒绝 clear 命令,无需任何预配置的作用域。 |
|
启用 delete 命令,无需任何预配置的作用域。 |
|
拒绝 delete 命令,无需任何预配置的作用域。 |
|
启用 entries 命令,无需任何预配置的作用域。 |
|
拒绝 entries 命令,无需任何预配置的作用域。 |
|
启用 get 命令,无需任何预配置的作用域。 |
|
拒绝 get 命令,无需任何预配置的作用域。 |
|
启用 get_store 命令,无需任何预配置的作用域。 |
|
拒绝 get_store 命令,无需任何预配置的作用域。 |
|
启用 has 命令,无需任何预配置的作用域。 |
|
拒绝 has 命令,无需任何预配置的作用域。 |
|
启用 keys 命令,无需任何预配置的作用域。 |
|
拒绝 keys 命令,无需任何预配置的作用域。 |
|
启用 length 命令,无需任何预配置的作用域。 |
|
拒绝 length 命令,无需任何预配置的作用域。 |
|
启用 load 命令,无需任何预配置的作用域。 |
|
拒绝 load 命令,无需任何预配置的作用域。 |
|
启用 reload 命令,无需任何预配置的作用域。 |
|
拒绝 reload 命令,无需任何预配置的作用域。 |
|
启用 reset 命令,无需任何预配置的作用域。 |
|
拒绝 reset 命令,无需任何预配置的作用域。 |
|
启用 save 命令,无需任何预配置的作用域。 |
|
拒绝 save 命令,无需任何预配置的作用域。 |
|
启用 set 命令,无需任何预配置的作用域。 |
|
拒绝 set 命令,无需任何预配置的作用域。 |
|
启用 values 命令,无需任何预配置的作用域。 |
|
拒绝 values 命令,无需任何预配置的作用域。 |
© 2025 Tauri Contributors. CC-BY / MIT