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
,僅在首次存取時載入 store
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 貢獻者。CC-BY / MIT