跳到內容
Tauri

Store

此外掛程式提供持久性的鍵值儲存。這是處理應用程式狀態的眾多選項之一。請參閱狀態管理概觀以取得其他選項的更多資訊。

此儲存區可讓您將狀態持久儲存到檔案中,該檔案可以隨需儲存和載入,包括在應用程式重新啟動之間。請注意,此程序是異步的,需要在您的程式碼中處理。它可以在 webview 或 Rust 中使用。

支援平台

此外掛程式需要至少 1.77.2 的 Rust 版本

平台 層級 注意事項
windows
linux
macos
android
ios

設定

安裝 store 外掛程式以開始使用。

使用您專案的套件管理器來新增依賴項

npm run tauri add 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 created
const 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();

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';

權限

預設情況下,所有潛在危險的外掛程式命令和作用域都被封鎖,無法存取。您必須修改 capabilities 組態中的權限才能啟用這些。

請參閱功能概觀以取得更多資訊,以及逐步指南以使用外掛程式權限。

src-tauri/capabilities/default.json
{
"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

權限表

識別碼 描述

store:allow-clear

啟用 clear 命令,而無需任何預先設定的作用域。

store:deny-clear

拒絕 clear 命令,而無需任何預先設定的作用域。

store:allow-delete

啟用 delete 命令,而無需任何預先設定的作用域。

store:deny-delete

拒絕 delete 命令,而無需任何預先設定的作用域。

store:allow-entries

啟用 entries 命令,而無需任何預先設定的作用域。

store:deny-entries

拒絕 entries 命令,而無需任何預先設定的作用域。

store:allow-get

啟用 get 命令,而無需任何預先設定的作用域。

store:deny-get

拒絕 get 命令,而無需任何預先設定的作用域。

store:allow-get-store

啟用 get_store 命令,而無需任何預先設定的作用域。

store:deny-get-store

拒絕 get_store 命令,而無需任何預先設定的作用域。

store:allow-has

啟用 has 命令,而無需任何預先設定的作用域。

store:deny-has

拒絕 has 命令,而無需任何預先設定的作用域。

store:allow-keys

啟用 keys 命令,而無需任何預先設定的作用域。

store:deny-keys

拒絕 keys 命令,而無需任何預先設定的作用域。

store:allow-length

啟用 length 命令,而無需任何預先設定的作用域。

store:deny-length

拒絕 length 命令,而無需任何預先設定的作用域。

store:allow-load

啟用 load 命令,而無需任何預先設定的作用域。

store:deny-load

拒絕 load 命令,而無需任何預先設定的作用域。

store:allow-reload

啟用 reload 命令,而無需任何預先設定的作用域。

store:deny-reload

拒絕 reload 命令,而無需任何預先設定的作用域。

store:allow-reset

啟用 reset 命令,而無需任何預先設定的作用域。

store:deny-reset

拒絕 reset 命令,而無需任何預先設定的作用域。

store:allow-save

啟用 save 命令,而無需任何預先設定的作用域。

store:deny-save

拒絕 save 命令,而無需任何預先設定的作用域。

store:allow-set

啟用 set 命令,而無需任何預先設定的作用域。

store:deny-set

拒絕 set 命令,而無需任何預先設定的作用域。

store:allow-values

啟用 values 命令,而無需任何預先設定的作用域。

store:deny-values

拒絕 values 命令,而無需任何預先設定的作用域。


© 2025 Tauri 貢獻者。CC-BY / MIT