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

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

阅读 477
1 个回答

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=''
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏