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> = {}
}
相关推荐
高心星4 小时前
HarmonyOS 5.0应用开发——MVVM模式的应用
harmonyos·mvvm·鸿蒙5.0·备忘录应用
别说我什么都不会4 小时前
【仓颉三方库】工具类—— compress4cj
harmonyos
别说我什么都不会4 小时前
【仓颉三方库】工具类—— uuid4cj
harmonyos
__Benco11 小时前
OpenHarmony - 小型系统内核(LiteOS-A)(十),魔法键使用方法,用户态异常信息说明
人工智能·harmonyos
NapleC1 天前
HarmonyOS:一多能力介绍:一次开发,多端部署
华为·harmonyos
我爱学习_zwj1 天前
【鸿蒙HarmonyOS】深入理解router与Navigation
华为·harmonyos
NapleC1 天前
HarmonyOS:1.7
harmonyos
鸿蒙布道师1 天前
鸿蒙NEXT开发通知工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
鸿蒙布道师1 天前
鸿蒙NEXT开发网络相关工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei