HarmonyOS UIAbility 中初始化 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) { 
  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.'); 
}); 
//这里就正常 
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; 
}); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
HarmonyOS
2024-11-22 09:35:22
892浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

​放入位置不一样报错,是传入的context有问题。存储Map对象,可以尝试使用treeMap,参考链接:​https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-treemap-V5

let treeMap: TreeMap<string, number> = new TreeMap(); 
treeMap.set("squirrel", 123); 
treeMap.set("sparrow", 356); 
let result = treeMap.get("sparrow"); 
  • 1.
  • 2.
  • 3.
  • 4.
分享
微博
QQ
微信
回复
2024-11-22 17:32:58
相关问题
HarmonyOS @ohos.data.preferences
1356浏览 • 1回复 待解决
HarmonyOS @ohos.data.preferences 数据处理
1207浏览 • 1回复 待解决
HarmonyOS XComponent初始化
1115浏览 • 1回复 待解决
HarmonyOS 组件初始化参数
713浏览 • 1回复 待解决
HarmonyOS struct初始化方法
971浏览 • 1回复 待解决
HarmonyOS Component初始化问题
1471浏览 • 1回复 待解决
HarmonyOS taskpoolnew 对象报错未初始化
1146浏览 • 1回复 待解决
HarmonyOS 初始化EGL环境失败
894浏览 • 1回复 待解决
HarmonyOS 应用RDB初始化示例
1239浏览 • 1回复 待解决
HarmonyOS 推送服务初始化失败
825浏览 • 1回复 待解决
dataPreferences.Preferences取不到数据
1291浏览 • 0回复 待解决
HarmonyOS 怎么监听Web初始化完成?
956浏览 • 1回复 待解决