HarmonyOS navigation的getParam问题

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

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

HarmonyOS
2024-12-25 12:55:52
浏览
收藏 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);
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
分享
微博
QQ
微信
回复
2024-12-25 15:35:55
相关问题
HarmonyOS 关于Navigation组件问题
1453浏览 • 1回复 待解决
HarmonyOS Navigation组件使用问题
718浏览 • 1回复 待解决
HarmonyOS关于navigation问题
1482浏览 • 1回复 待解决
HarmonyOS Navigation使用问题
1257浏览 • 1回复 待解决
HarmonyOS Navigation路由问题
720浏览 • 1回复 待解决
HarmonyOS Navigation 使用问题
1177浏览 • 1回复 待解决
Navigation页面跳转问题
1338浏览 • 1回复 待解决
HarmonyOS Navigation折叠屏适配问题
852浏览 • 1回复 待解决
HarmonyOS 关于navigation和router问题
748浏览 • 1回复 待解决
HarmonyOS Navigation传参问题
1181浏览 • 1回复 待解决
HarmonyOS Navigation组件hideNavBar问题
631浏览 • 1回复 待解决
HarmonyOSNavigation显示dialog问题
1409浏览 • 1回复 待解决
HarmonyOS Navigation和Tab组件问题
2056浏览 • 1回复 待解决
HarmonyOS Navigation动态跳转页面问题
812浏览 • 1回复 待解决
HarmonyOS Navigation生命周期问题
691浏览 • 1回复 待解决