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

HarmonyOS
2天前
浏览
收藏 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.');
    });
  }

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

let storage=LocalStorage.GetShared()
@Entry(storage)
struct Index{
  @LocalStorageLink('targetPageType') pageType:string=''
分享
微博
QQ
微信
回复
2天前
相关问题
如何page获取WindowStage
815浏览 • 1回复 待解决
page怎么获取WindowStage
1853浏览 • 1回复 待解决
Page Ability 无法实现跳转
8784浏览 • 1回复 待解决
HarmonyOS rest参数如何传递
63浏览 • 1回复 待解决
HarmonyOS page 参数问题
361浏览 • 1回复 待解决
HarmonyOS Page如何拦截系统返回
33浏览 • 1回复 待解决
HarmonyOS rcp请求如何传递参数
22浏览 • 1回复 待解决