HarmonyOS pushDestinationByName下个界面返回时候怎么回传值

HarmonyOS
2024-12-18 16:48:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa
class PageParam {
  constructor(num_: number) {
    this.num = num_;
  }

  num: number = 0;
}

class BackParam {
  constructor(backStr_: string) {
    this.backStr = backStr_;
  }

  backStr: string = "";
}

@Component
struct PageOne {
  private stack: NavPathStack | null = null;
  private name: string = "";
  private paramNum: number = 0;
  @State backStr: string = "暂无信息"

  build() {
    NavDestination() {
      Column() {
        Text("NavPathInfo: name: " + this.name + ", paramNum: " + this.paramNum)
        Text(this.backStr)

        Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            if (this.stack) {
              let p = new PageParam(this.paramNum + 1);
              this.stack.pushPath({
                name: "pageOne", param: p, onPop: (popInfo: PopInfo) => {
                  console.info(' result: ' + JSON.stringify(popInfo.result));
                  this.backStr = popInfo.result['backStr'] as string
                }
              });
            }
          })
        Button('pop', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.stack?.pop(new BackParam("返回信息" + this.paramNum))
          })
      }
      .width('100%')
      .height('100%')
    }
    .hideTitleBar(true)
    .title('pageOne')
    .onReady((ctx: NavDestinationContext) => {
      // 在NavDestination中能够拿到传来的NavPathInfo和当前所处的NavPathStack
      try {
        this.name = ctx?.pathInfo?.name;
        this.paramNum = (ctx?.pathInfo?.param as PageParam)?.num;
        this.stack = ctx.pathStack;
      } catch (e) {
        console.log(`testTag onReady catch exception: ${JSON.stringify(e)}`)
      }
    })
  }
}

@Entry
@Component
struct NavigationExample2 {
  private stack: NavPathStack = new NavPathStack();
  @State backStr: string = "暂无信息"

  @Builder
  PageMap(name: string) {
    if (name === 'pageOne') {
      PageOne()
    }
  }

  build() {
    Navigation(this.stack) {
      Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center }) {
        Text(this.backStr)
        Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            let p = new PageParam(1);
            this.stack.pushPath({
              name: "pageOne", param: p, onPop: (popInfo: PopInfo) => {
                console.info(' result: ' + JSON.stringify(popInfo.result));
                this.backStr = popInfo.result['backStr'] as string
              }
            })
          })
      }
      .width('100%')
      .height('100%')
    }
    .width('100%')
    .height('100%')
    .navDestination(this.PageMap)
    // .hideNavBar(true)
    .title('Navigation')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-18 18:39:08


相关问题
HarmonyOS 界面逆向
845浏览 • 1回复 待解决
HarmonyOS navigation页面之间
453浏览 • 1回复 待解决
HarmonyOS 页面反向怎么
539浏览 • 1回复 待解决
HarmonyOS 页面
545浏览 • 1回复 待解决
HarmonyOS Navigation数据
307浏览 • 1回复 待解决
arkts父子组件组件怎么通信啊?
5959浏览 • 1回复 待解决
HarmonyOS Component问题
377浏览 • 2回复 待解决
HarmonyOS Checkbox如何动态
405浏览 • 1回复 待解决
HarmonyOS 父子组件问题
411浏览 • 1回复 待解决
Js FA拉起Java FA时候怎么参?
5001浏览 • 1回复 待解决
HarmonyOS AKI是否支持引用
330浏览 • 1回复 待解决
HarmonyOS 如何实现页面反向
777浏览 • 1回复 待解决
HarmonyOS原生如何给flutter
872浏览 • 1回复 待解决
HarmonyOS Slider调问题
467浏览 • 1回复 待解决
HarmonyOS router跳转返回如何
358浏览 • 1回复 待解决