如何使用 `AppStorageV2` 存储变量
如何使用 AppStorageV2
存储变量
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
// 入口默认加载的页面
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
// 1. 设置窗口全屏
let isLayoutFullScreen = true;
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => {
console.info('Succeeded in setting the window layout to full-screen mode.');
}).catch((err: BusinessError) => {
console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
});
// 2. 获取布局避让遮挡的区域
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
let avoidArea = windowClass.getWindowAvoidArea(type);
let bottomRectHeight = avoidArea.bottomRect.height; // 获取到导航条区域的高度
// AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);
type = window.AvoidAreaType.TYPE_SYSTEM; // 以状态栏避让为例
avoidArea = windowClass.getWindowAvoidArea(type);
let topRectHeight = avoidArea.topRect.height; // 获取状态栏区域高度
// AppStorage.setOrCreate('topRectHeight', topRectHeight);
// 3. 注册监听函数,动态获取避让区域数据
windowClass.on('avoidAreaChange', (data) => {
if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
let topRectHeight = data.area.topRect.height;
//
} else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
let bottomRectHeight = data.area.bottomRect.height;
//
// AppStorageV2.connect(AvoidAreaInfo, () => new AvoidAreaInfo())!
}
});
}
如何使用 AppStorageV2
来存取 bottomRectHeight
& topRectHeight
这两个变量呢 ,文档写的有点看不明白 求大佬指点一下
@ObservedV2
export class AvoidAreaInfo {
@Trace topRectHeight: number = 0;
@Trace bottomRectHeight: number = 0;
}
2 回复
AppStorageV2
是鸿蒙系统中用于持久化存储应用数据的工具。你可以通过 AppStorageV2.setOrCreate()
方法存储变量,使用 AppStorageV2.get()
方法读取变量。存储时需指定键名和值,读取时通过键名获取值。数据存储在应用的私有目录中,支持基本数据类型和复杂对象。
要在HarmonyOS Next中使用AppStorageV2
存储变量,可以按照以下步骤操作:
- 首先定义Observed类:
@ObservedV2
export class AvoidAreaInfo {
@Trace topRectHeight: number = 0;
@Trace bottomRectHeight: number = 0;
}
- 在Ability中初始化并连接AppStorageV2:
// 初始化AppStorageV2
AppStorageV2.connect(AvoidAreaInfo, () => new AvoidAreaInfo());
// 获取实例
const avoidAreaInfo = AppStorageV2.Get(AvoidAreaInfo);
- 存储变量:
// 存储状态栏高度
avoidAreaInfo.topRectHeight = topRectHeight;
// 存储导航栏高度
avoidAreaInfo.bottomRectHeight = bottomRectHeight;
- 在UI组件中使用:
// 获取存储的值
const topHeight = avoidAreaInfo.topRectHeight;
const bottomHeight = avoidAreaInfo.bottomRectHeight;
- 动态更新时直接修改属性值即可,UI会自动刷新:
windowClass.on('avoidAreaChange', (data) => {
if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
avoidAreaInfo.topRectHeight = data.area.topRect.height;
} else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
avoidAreaInfo.bottomRectHeight = data.area.bottomRect.height;
}
});
关键点:
- 使用
@ObservedV2
和@Trace
装饰器标记类和属性 - 通过
AppStorageV2.connect
初始化 - 使用
AppStorageV2.Get
获取实例 - 修改属性值会自动触发UI更新,