Skip to content
Tauri 中文网

商店

此插件提供持久键值存储。这是处理应用中状态的众多选项之一。有关 Windows 安装程序分发选项和配置的更多信息,请参阅 状态管理概述

¥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.

此存储将允许你将状态保存到文件中,该文件可以根据需要保存和加载,包括在应用重新启动之间。请注意,此过程是异步的,需要在你的代码中处理它。它可以在 Web 视图中或 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.

使用项目的包管理器添加依赖:

¥Use your project’s package manager to add the dependency:

npm run tauri add 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 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,它仅在第一次访问时加载商店

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

从 v1 和 v2 beta/rc 迁移

¥Migrating from v1 and v2 beta/rc

import { Store } from '@tauri-apps/plugin-store';
import { LazyStore } from '@tauri-apps/plugin-store';

权限

¥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.

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

  • 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

Permission Table

Identifier Description

store:allow-clear

Enables the clear command without any pre-configured scope.

store:deny-clear

Denies the clear command without any pre-configured scope.

store:allow-delete

Enables the delete command without any pre-configured scope.

store:deny-delete

Denies the delete command without any pre-configured scope.

store:allow-entries

Enables the entries command without any pre-configured scope.

store:deny-entries

Denies the entries command without any pre-configured scope.

store:allow-get

Enables the get command without any pre-configured scope.

store:deny-get

Denies the get command without any pre-configured scope.

store:allow-get-store

Enables the get_store command without any pre-configured scope.

store:deny-get-store

Denies the get_store command without any pre-configured scope.

store:allow-has

Enables the has command without any pre-configured scope.

store:deny-has

Denies the has command without any pre-configured scope.

store:allow-keys

Enables the keys command without any pre-configured scope.

store:deny-keys

Denies the keys command without any pre-configured scope.

store:allow-length

Enables the length command without any pre-configured scope.

store:deny-length

Denies the length command without any pre-configured scope.

store:allow-load

Enables the load command without any pre-configured scope.

store:deny-load

Denies the load command without any pre-configured scope.

store:allow-reload

Enables the reload command without any pre-configured scope.

store:deny-reload

Denies the reload command without any pre-configured scope.

store:allow-reset

Enables the reset command without any pre-configured scope.

store:deny-reset

Denies the reset command without any pre-configured scope.

store:allow-save

Enables the save command without any pre-configured scope.

store:deny-save

Denies the save command without any pre-configured scope.

store:allow-set

Enables the set command without any pre-configured scope.

store:deny-set

Denies the set command without any pre-configured scope.

store:allow-values

Enables the values command without any pre-configured scope.

store:deny-values

Denies the values command without any pre-configured scope.


Tauri 中文网 - 粤ICP备13048890号