HarmonyOS 在Ability中windowStage.loadContent('pages/MeetingPage')如何传递参数给page

HarmonyOS
2024-12-25 12:35:51
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

1.在ability中声明并设置 LocalStorage 的值,在loadContent 时将LocalStorage 做为参数传

export default class EntryAbility extends UIAbility {
  private localStorage:LocalStorage=new LocalStorage()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    let targetPageType=want?.parameters?.['targetPageType']
    this.localStorage.setOrCreate('targetPageType',targetPageType)
  }

  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',this.localStorage, (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.');
    });
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

2.在ets中接受参数,如果ts中没有设置,就会使用默认值,pageType的默认值是空字

let storage=LocalStorage.GetShared()
@Entry(storage)
struct Index{
  @LocalStorageLink('targetPageType') pageType:string=''
  • 1.
  • 2.
  • 3.
  • 4.
分享
微博
QQ
微信
回复
2024-12-25 14:41:57
相关问题
如何page获取WindowStage
1401浏览 • 1回复 待解决
page怎么获取WindowStage
2449浏览 • 1回复 待解决
HarmonyOS page 参数问题
793浏览 • 1回复 待解决
HarmonyOS rest参数如何传递
949浏览 • 1回复 待解决
Page Ability 无法实现跳转
9854浏览 • 1回复 待解决
HarmonyOS Page如何拦截系统返回
612浏览 • 1回复 待解决
HarmonyOS rcp请求如何传递参数
835浏览 • 1回复 待解决