在HarmonyOS开发中初始化 dataPreferences from '@ohos.data.preferences'? 为什么在windowStage.loadContent之前初始化会报错,而放到后面就正常?
async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
// async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
const windowBarMag = new WindowBarManager();
windowBarMag.immersiveWindow(windowStage, Const.TRANSPARENT_COLOR, true);
// 报错 await DataManager.getInstance().initStore("TeacherStore");
windowStage.loadContent('pages/SplashPage', (err) => {
if (err.code) {
return;
}
});
//这里就正常
await DataManager.getInstance().initStore("TeacherStore");
}
dataPreferences from '@ohos.data.preferences';
initStore(storeName: string): Promise<void> {
this.storeName = storeName;
return dataPreferences.getPreferences(this.context, this.storeName)
.then((preferences: dataPreferences.Preferences) => {
this.preferencesSp = preferences;
});
}
本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。
放入位置不一样报错,是传入的context有问题。存储Map对象,可以尝试使用treeMap,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...
本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。