HarmonyOS LocalStorage使用

官方关于LocalStorage的说明: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/arkts-localstorage-0000001524537149-V2

说了些页面间,组建内关于LocalStorage的使用方法。这里就不再赘述。

但是关于如何在 UIAbility和 Page之间传递没有说。为啥我会这么问呢,因为我需要这么传:

javascript 复制代码
onWindowStageCreate(windowStage: window.WindowStage): void {
    windowStage.loadContent('pages/MainPage', localStorage);
  }

我需要把 UIAbility中的参数传递到第一个需要加载到页面中去,而且 windowStage.loadContent() 方法本来就可以传递一个 localStorage 的。

也就是如何将LocalStorage实例从UIAbility共享到一个或多个视图。

javascript 复制代码
export default class MainAbility extends UIAbility {

  localStorage = new LocalStorage();

  async onCreate(want: Want): Promise<void> {
    if (want?.uri != null && want.uri.length > 0) {
      let routePage = want.parameters?.['page'] as string;
      let routePageParam = want.parameters as Record<string, object>

      this.localStorage.clear()
      this.localStorage.setOrCreate('page', routePage)
      this.localStorage.setOrCreate('params', routePageParam)
    }
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    windowStage.loadContent('pages/MainPage', this.localStorage);
  }
}

页面中:

javascript 复制代码
let storage = LocalStorage.getShared()
@Entry(storage)
@Component
struct MainPage {

  @State notificationState: string = ''
  private clickBackTimeRecord: number = 0;
  @LocalStorageProp('page') page: string = ''
  @LocalStorageProp('params') params: Record<string, object> = {}
}
相关推荐
不凡的凡2 小时前
鸿蒙图片相似性对比
华为·harmonyos
Georgewu4 小时前
【HarmonyOS】HAR和HSP循环依赖和依赖传递问题详解
harmonyos
Georgewu9 天前
【HarmonyOS 5】鸿蒙跨平台开发方案详解(一)
flutter·harmonyos
万少9 天前
重磅推出 🔥 HarmonyOS AI 助手 CodeGenie V6 的使用教程
前端·harmonyos
我睡醒再说9 天前
纯血HarmonyOS5 打造小游戏实践:绘画板(附源文件)
harmonyos
我睡醒再说9 天前
HarmonyOS 5 ArkTS Worker线程:构建高性能移动应用的并行计算引擎
harmonyos
我睡醒再说9 天前
纯血HarmonyOS5 打造小游戏实践:扫雷(附源文件)
harmonyos
二流小码农9 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
Moximxxx10 天前
Harmory Next 下拉刷新 悬浮loading效果
harmonyos
中雨202510 天前
HarmonyOS Next快速入门:类型定义和国际化
harmonyos