@napacitor/preferences
键值持久化存储插件(与 @capacitor/preferences 对应)。
安装方式
To use npm:
npm install @napacitor/preferences
To use yarn:
yarn add @napacitor/preferences
Sync native files:
npx nap sync
用法
import { Preferences } from '@napacitor/preferences';
// 可选:配置存储组(后续读写清除均作用于该组;省略时使用默认存储)
await Preferences.configure({ group: 'session' });
await Preferences.set({ key: 'name', value: 'Max' });
const { value } = await Preferences.get({ key: 'name' }); // 'Max'
await Preferences.remove({ key: 'name' });
await Preferences.clear();
// Capacitor 2 Storage 兼容(Napacitor 无迁移源:migrate 返回空结果,removeOld 为 no-op)
const { migrated, existing } = await Preferences.migrate(); // { migrated: [], existing: [] }
await Preferences.removeOld();
实现原理
封装 .NET MAUI 的 Preferences 文档所述 API:Preferences.Default.Get / Set / Remove / Clear(iOS:UserDefaults / Android:SharedPreferences / Windows:ApplicationData)。configure({group}) 映射 MAUI 的 sharedName(Android 对应 suite;跨应用共享仅在 Android 有原生语义),作用于后续全部读写清除;web 端映射为 localStorage 键前缀(默认 napacitor.)。MAUI Preferences 不提供键枚举能力,故本插件不提供 keys()(需要键清单请自行维护)。
权限
Android
无需额外配置。
iOS
无需额外配置。
Windows
无需额外配置。
API
configure(options)
运行时配置存储组(后续读写清除均作用于该组)。
| 参数 | 类型 |
|---|---|
options | ConfigureOptions |
返回值: Promise<void>
get(options)
读取键值。
| 参数 | 类型 |
|---|---|
options | GetOptions |
返回值: Promise<GetResult>
set(options)
写入键值。
| 参数 | 类型 |
|---|---|
options | SetOptions |
返回值: Promise<void>
remove(options)
删除指定键。
| 参数 | 类型 |
|---|---|
options | RemoveOptions |
返回值: Promise<void>
clear()
清空当前存储组(注意:直接映射 Preferences.Clear(sharedName),清空整个存储组,包含非本插件写入的数据)。
返回值: Promise<void>
migrate()
从 Capacitor 2 Storage 迁移。Napacitor 无迁移源,恒返回空结果。
返回值: Promise<MigrateResult>
removeOld()
删除 Capacitor 2 Storage 的旧数据(Napacitor 无迁移源,no-op)。
返回值: Promise<void>
Interfaces
ConfigureOptions
| 属性 | 类型 | 描述 |
|---|---|---|
group | string | 存储组名(映射 MAUI Preferences 的 sharedName);省略时使用默认存储。 |
GetOptions
| 属性 | 类型 | 描述 |
|---|---|---|
key | string | 要读取的键(不能为空,否则抛 INVALID_ARGUMENTS)。 |