HarmonyOS Navigation使用系统路由表,页面无法接收到参数?

如题:HarmonyOS Navigation使用系统路由表,页面无法接收到参数?

阅读 558
1 个回答

参考以下demo

//index.ets
@Builder
routerMap(builderName: string, param: object) {
  if (builderName === 'featureA') {
    FeatureIndex(param);
  }
};

aboutToAppear(): void {
  this.entryHapRouter.pushPathByName( "featureA", "测试", true)
}

//FeatureIndex.ets
@Builder
export function FeatureIndex(value: object) {
  NavDestination() {
    Column() {
      Text('Hello FeatureA Page')
      Text(`传入的参数:${JSON.stringify(value)}`)
        .margin(20)
    }
    .width('100%')
    .height('100%')
  }
  .hideTitleBar(true)
}


//路由表的方式传递参数:
//index.ets
@Entry
@Component
struct NavigationExample {
  //绑定 NavPathStack
  @Provide('NavPathStack')pageInfo: NavPathStack = new NavPathStack()

  build() {
    Navigation(this.pageInfo) {
      Column() {
        Button('StartTest', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfo.pushPath({ name: 'pageOne',param:"测试参数" });
          })
      }
    }.title('NavIndex')
    .backgroundColor(Color.Orange)

  }
}


//PageOne.ets 子页面绑定NavPathStack
@Consume('NavPathStack') pageInfo: NavPathStack;

aboutToAppear(): void {
  this.message = this.pageInfo.getParamByIndex(0) as string
  console.log(JSON.stringify(this.pageInfo));
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进