HarmonyOS Navigation使用自定义NavPathStack不生效问题

自定义一个 MyPathStack extends NavPathStack, 在Navigation所在页面中创建实例, 并传递给Navigation(this.pathStack), 在后续使用时, this.pathStack.pushPath 不生效; 如果将 MyPathStack 替换为直接使用 NavPathStack, this.pathStack.pushPath 生效; 因此怀疑自定义组件不可用

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

该示例主要演示如下两点功能:

  1. 如何定义NavPathStack的派生类

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

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) {
      Row({}) {
        Column() {
          Text('jump')
            .onClick(()=>{
              this.jumpMainPage()
            })
        }
        .width('100%').height(98)
        .backgroundColor(Color.Grey)
      }
      .width('100%').height('100%').alignItems(VerticalAlign.Bottom)
      .backgroundColor(Color.Green)

    }.hideTitleBar(true)
    .navDestination(this.pageMap)
  }

  jumpMainPage() {
    // router.replaceUrl({ url: 'pages/Index' });
    this.derivedStack.pushPath({ name: '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天前
相关问题
自定义弹窗使用相关问题
972浏览 • 1回复 待解决
NavPathStack使用问题
1691浏览 • 1回复 待解决
HarmonyOS WebView使用自定义键盘问题
94浏览 • 1回复 待解决
自定义装饰器的使用问题
832浏览 • 1回复 待解决
如何使用NavigationnavPathStack参数
1477浏览 • 1回复 待解决
HarmonyOS margin生效问题
102浏览 • 1回复 待解决
HarmonyOS @Observed生效问题
481浏览 • 1回复 待解决
HarmonyOS targetSpace生效问题
28浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
418浏览 • 1回复 待解决
HarmonyOS 关于onKeyEvent生效问题
385浏览 • 1回复 待解决
HarmonyOS 使用自定义字体
268浏览 • 1回复 待解决