@capacitor/storage

中文文档
1.2.5发布于 2022/03/04MITTypeScriptESM + CJS零依赖近 30 天下载 2.2万

The Storage API provides a simple key/value persistent store for lightweight data.

此软件包已被标记为弃用

package has been renamed to @capacitor/preferences

原始使用文档
文档目录13 节
  1. Install
  2. Example
  3. Working with JSON
  4. API
  5. configure(...)
  6. get(...)
  7. set(...)
  8. remove(...)
  9. clear()
  10. keys()
  11. migrate()
  12. removeOld()
  13. Interfaces

@capacitor/storage

The Storage API provides a simple key/value persistent store for lightweight data.

Mobile OSs may periodically clear data set in window.localStorage, so this API should be used instead. This API will fall back to using localStorage when running as a Progressive Web App.

This plugin will use UserDefaults on iOS and SharedPreferences on Android. Stored data is cleared if the app is uninstalled.

Note: This API is not meant to be used as a local database. If your app stores a lot of data, has high read/write load, or requires complex querying, we recommend taking a look at a SQLite-based solution. One such solution is Ionic Secure Storage, a SQLite-based engine with full encryption support. The Capacitor Community has also built a number of other storage engines.

Install

npm install @capacitor/storage
npx cap sync

Example

import { Storage } from '@capacitor/storage';

const setName = async () => {
  await Storage.set({
    key: 'name',
    value: 'Max',
  });
};

const checkName = async () => {
  const { value } = await Storage.get({ key: 'name' });

  console.log(`Hello ${value}!`);
};

const removeName = async () => {
  await Storage.remove({ key: 'name' });
};

Working with JSON

The Storage API only supports string values. You can, however, use JSON if you JSON.stringify the object before calling set(), then JSON.parse the value returned from get().

This method can also be used to store non-string values, such as numbers and booleans.

API

configure(...)

configure(options: ConfigureOptions) => Promise<void>

Configure the storage plugin at runtime.

Options that are undefined will not be used.

ParamType
optionsConfigureOptions

Since: 1.0.0


get(...)

get(options: GetOptions) => Promise<GetResult>

Get the value from storage of a given key.

ParamType
optionsGetOptions

Returns: Promise<GetResult>

Since: 1.0.0


set(...)

set(options: SetOptions) => Promise<void>

Set the value in storage for a given key.

ParamType
optionsSetOptions

Since: 1.0.0


remove(...)

remove(options: RemoveOptions) => Promise<void>

Remove the value from storage for a given key, if any.

ParamType
optionsRemoveOptions

Since: 1.0.0


clear()

clear() => Promise<void>

Clear keys and values from storage.

Since: 1.0.0


keys()

keys() => Promise<KeysResult>

Return the list of known keys in storage.

Returns: Promise<KeysResult>

Since: 1.0.0


migrate()

migrate() => Promise<MigrateResult>

Migrate data from the Capacitor 2 Storage plugin.

This action is non-destructive. It will not remove old data and will only write new data if they key was not already set. To remove the old data after being migrated, call removeOld().

Returns: Promise<MigrateResult>

Since: 1.0.0


removeOld()

removeOld() => Promise<void>

Removes old data with _cap_ prefix from the Capacitor 2 Storage plugin.

Since: 1.1.0


Interfaces

ConfigureOptions

PropTypeDescriptionDefaultSince
groupstringSet the storage group. Storage groups are used to organize key/value pairs. Using the value 'NativeStorage' provides backwards-compatibility with cordova-plugin-nativestorage. WARNING: The clear() method can delete unintended values when using the 'NativeStorage' group.CapacitorStorage1.0.0

GetResult

PropTypeDescriptionSince
valuestring | nullThe value from storage associated with the given key. If a value was not previously set or was removed, value will be null.1.0.0

GetOptions

PropTypeDescriptionSince
keystringThe key whose value to retrieve from storage.1.0.0

SetOptions

PropTypeDescriptionSince
keystringThe key to associate with the value being set in storage.1.0.0
valuestringThe value to set in storage with the associated key.1.0.0

RemoveOptions

PropTypeDescriptionSince
keystringThe key whose value to remove from storage.1.0.0

KeysResult

PropTypeDescriptionSince
keysstring[]The known keys in storage.1.0.0

MigrateResult

PropTypeDescriptionSince
migratedstring[]An array of keys that were migrated.1.0.0
existingstring[]An array of keys that were already migrated or otherwise exist in storage that had a value in the Capacitor 2 Storage plugin.1.0.0

版本与发布记录

@capacitor/storage 共发布 29 个版本,首次发布于 2020/09/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.5latest2022/03/044 年前
1.2.42022/01/204 年前
1.2.32021/11/035 年前
1.2.22021/10/155 年前
1.2.02021/09/165 年前
1.1.02021/09/025 年前

关于 @capacitor/storage 的常见问题

@capacitor/storage 是什么?

The Storage API provides a simple key/value persistent store for lightweight data.注意:该包已被作者标记为弃用——package has been renamed to @capacitor/preferences

如何安装 @capacitor/storage?

在项目目录运行 npm install @capacitor/storage,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(^3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capacitor/storage 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capacitor/storage 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capacitor/storage 可以商用吗?

@capacitor/storage 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor/storage 的下载量有多少?

根据 npm 官方下载统计,@capacitor/storage 在 2026/10/09 – 2026/11/07 这 30 天内被下载 21,907 次,日均约 730 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capacitor/storage 还在维护吗?

最新版本 1.2.5 发布于 2022/03/04(4 年前);至今共发布 29 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/09/01。作者已将其标记为弃用,新项目建议寻找替代方案。