商店
这个插件提供了一个持久化的键值存储。这是处理应用中状态的众多选项之一。有关其他选项的更多信息,请参见state management overview。
🌐 This plugin provides a persistent key-value store. This is one of many options to handle state in your application. See the state management overview for more information on additional options.
此存储将允许你将状态持久化到文件中,该文件可以按需保存和加载,包括在应用重启之间。请注意,此过程是异步的,因此需要在你的代码中进行处理。它既可以在网页视图中使用,也可以在 Rust 中使用。
🌐 This store will allow you to persist state to a file which can be saved and loaded on demand including between app restarts. Note that this process is asynchronous which will require handling it within your code. It can be used both in the webview or within Rust.
🌐 Supported Platforms
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
🌐 Setup
安装 store 插件即可开始使用。
🌐 Install the store plugin to get started.
使用项目的包管理器添加依赖:
npm run tauri add storeyarn run tauri add storepnpm tauri add storedeno task tauri add storebun tauri add storecargo 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
🌐 Usage
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 `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,它只在首次访问时加载存储
🌐 There’s also a high level JavaScript API LazyStore which only loads the store on first access
import { LazyStore } from '@tauri-apps/plugin-store';
const store = new LazyStore('settings.json');🌐 Migrating from v1 and 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 }));🌐 Permissions
默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。
🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南。
🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "permissions": [ ..., "store:default", ]}Default Permission
This permission set configures what kind of operations are available from the store plugin.
Granted Permissions
All operations are enabled by default.
This default permission set includes the following:
allow-loadallow-get-storeallow-setallow-getallow-hasallow-deleteallow-clearallow-resetallow-keysallow-valuesallow-entriesallow-lengthallow-reloadallow-save
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the clear command without any pre-configured scope. |
|
|
Denies the clear command without any pre-configured scope. |
|
|
Enables the delete command without any pre-configured scope. |
|
|
Denies the delete command without any pre-configured scope. |
|
|
Enables the entries command without any pre-configured scope. |
|
|
Denies the entries command without any pre-configured scope. |
|
|
Enables the get command without any pre-configured scope. |
|
|
Denies the get command without any pre-configured scope. |
|
|
Enables the get_store command without any pre-configured scope. |
|
|
Denies the get_store command without any pre-configured scope. |
|
|
Enables the has command without any pre-configured scope. |
|
|
Denies the has command without any pre-configured scope. |
|
|
Enables the keys command without any pre-configured scope. |
|
|
Denies the keys command without any pre-configured scope. |
|
|
Enables the length command without any pre-configured scope. |
|
|
Denies the length command without any pre-configured scope. |
|
|
Enables the load command without any pre-configured scope. |
|
|
Denies the load command without any pre-configured scope. |
|
|
Enables the reload command without any pre-configured scope. |
|
|
Denies the reload command without any pre-configured scope. |
|
|
Enables the reset command without any pre-configured scope. |
|
|
Denies the reset command without any pre-configured scope. |
|
|
Enables the save command without any pre-configured scope. |
|
|
Denies the save command without any pre-configured scope. |
|
|
Enables the set command without any pre-configured scope. |
|
|
Denies the set command without any pre-configured scope. |
|
|
Enables the values command without any pre-configured scope. |
|
|
Denies the values command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站