HarmonyOS navigation的getParam问题

1、如何定义NavPathStack的派生类。

2、派生类在Navigation中的基本用法。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

参考示例:

export class DerivedNavPathStack extends NavPathStack {
  // usr defined property 'id'
  id: string = "__default__"

  // new function in derived class
  setId(id: string) {
    this.id = id;
  }

  // new function in derived class
  getInfo(): string {
    return "this page used Derived NavPathStack, id: " + this.id
  }

  // overwrite function of NavPathStack
  getParent(): NavPathStack | null {
    return super.getParent();
  }

  getParam(index: number): ESObject {
    let params: ESObject = this.getParamByIndex(this.size() - 1) as ESObject
    return params
  }

  // overwrite and overload function of NavPathStack
  pop(animated?: boolean | undefined): NavPathInfo | undefined

  pop(result: Object, animated?: boolean | undefined): NavPathInfo | undefined

  pop(result?: Object, animated?: boolean | undefined): NavPathInfo | undefined {
    console.log('[derive-test] reached DerivedNavPathStack\'s pop');
    return super.pop(result, animated);
  }
}

class param {
  info: string = "__default_param__";

  constructor(info: string) {
    this.info = info
  }
}


@Entry
@Component
struct Index {
  derivedStack: DerivedNavPathStack = new DerivedNavPathStack();

  aboutToAppear(): void {
    this.derivedStack.setId('origin stack');
  }

  @Builder
  pageMap(name: string) {
    PageOne()
  }

  build() {
    Navigation(this.derivedStack) {
      Button('to Page One').margin(20).onClick(() => {
        this.derivedStack.pushPath({
          name: 'pageOne',
          param: new param('push pageOne in homePage')
        });
      })
    }.navDestination(this.pageMap)
    .title('Home Page')
  }
}

@Component
struct PageOne {
  derivedStack: DerivedNavPathStack = new DerivedNavPathStack();
  curStringifyParam: string = "NA";

  build() {
    NavDestination() {
      Column() {
        Text(this.derivedStack.getInfo())
          .margin(10)
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
          .textAlign(TextAlign.Start)
        Text('current page param info:')
          .margin(10)
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
          .textAlign(TextAlign.Start)
        Text(this.curStringifyParam)
          .margin(20)
          .fontSize(20)
          .textAlign(TextAlign.Start)
      }.backgroundColor(Color.Pink)

      Button('push Page One').margin(20).onClick(() => {
        this.derivedStack.pushPath({
          name: 'pageOne',
          param: new param('push pageOne in pageOne when stack size: ' + this.derivedStack.size())
        });
        let aa = this.derivedStack.getParam(this.derivedStack.size() - 1) as param
        console.log('tag size = ' + this.derivedStack.size() + '  aa = ' + JSON.stringify(aa))
      })
    }.onReady((context: NavDestinationContext) => {
      console.log('[derive-test] reached PageOne\'s onReady');
      // get derived stack from navdestinationContext
      this.derivedStack = context.pathStack as DerivedNavPathStack;
      console.log('[derive-test] -- got derivedStack: ' + this.derivedStack.id);
      this.curStringifyParam = JSON.stringify(context.pathInfo.param);
      console.log('[derive-test] -- got param: ' + this.curStringifyParam);
    })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 关于Navigation组件问题
495浏览 • 1回复 待解决
HarmonyOS Navigation组件使用问题
25浏览 • 1回复 待解决
HarmonyOS关于navigation问题
600浏览 • 1回复 待解决
Navigation页面跳转问题
488浏览 • 1回复 待解决
HarmonyOS Navigation使用问题
477浏览 • 1回复 待解决
HarmonyOS Navigation 使用问题
450浏览 • 1回复 待解决
HarmonyOS Navigation路由问题
83浏览 • 1回复 待解决
HarmonyOS Navigation折叠屏适配问题
51浏览 • 1回复 待解决
HarmonyOS Navigation传参问题
78浏览 • 1回复 待解决
HarmonyOS Navigation组件hideNavBar问题
64浏览 • 1回复 待解决
HarmonyOSNavigation显示dialog问题
516浏览 • 1回复 待解决
HarmonyOS Navigation生命周期问题
38浏览 • 1回复 待解决
HarmonyOS Navigation动态跳转页面问题
38浏览 • 1回复 待解决
HarmonyOS Navigation和Tab组件问题
973浏览 • 1回复 待解决