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

HarmonyOS
19h前
浏览
收藏 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')
  }
}
分享
微博
QQ
微信
回复
17h前
相关问题
HarmonyOS 界面逆向
379浏览 • 1回复 待解决
HarmonyOS 页面反向怎么
102浏览 • 1回复 待解决
arkts父子组件组件怎么通信啊?
5368浏览 • 1回复 待解决
Js FA拉起Java FA时候怎么参?
4564浏览 • 1回复 待解决
HarmonyOS原生如何给flutter
383浏览 • 1回复 待解决
HarmonyOS 如何实现页面反向
363浏览 • 1回复 待解决
HarmonyOS TextPickerDialog返回问题
309浏览 • 1回复 待解决
ArkTS如何进行页面间
1296浏览 • 1回复 待解决
OpenHarmony 使用WEB组件问题
3495浏览 • 1回复 待解决
HarmonyOS 调试的时候卡到启动界面
159浏览 • 1回复 待解决
ArkTS自定义组件如何父子间
393浏览 • 1回复 待解决
自定义组件的和绑定
942浏览 • 1回复 待解决
HarmonyOS 使用电池状态返回问题
395浏览 • 1回复 待解决
HarmonyOS 怎么用c++画界面
504浏览 • 1回复 待解决