HarmonyOS Navigation UI框架

任务子页面如何带参数回到主页面MainPage?

HarmonyOS
2024-10-17 10:57:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

Navigation传参可以参考官方示例:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#示例7

对示例代码做了点改动,首页获取子页面参数,可以通过onPop方法获取。完整代码:

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;  
  build() {  
    NavDestination() {  
      Column() {  
        Text("NavPathInfo: name: " + this.name + ", paramNum: " + this.paramNum)  
        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 });  
            }  
          })  
        Button('pop', { stateEffect: true, type: ButtonType.Capsule })  
          .width('80%')  
          .height(40)  
          .margin(20)  
          .onClick(() => {  
            this.stack?.pop(new BackParam("返回信息!!!!!!!!!!"))  
          })  
      }  
      .width('100%')  
      .height('100%')  
    }  
    .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));  
                console.info(JSON.stringify(popInfo.result['backStr']));  
                this.backStr = popInfo.result['backStr'] as string  
              }  
            })  
          })  
      }  
      .width('100%')  
      .height('100%')  
    }  
    .width('100%')  
    .height('100%')  
    .navDestination(this.PageMap)  
    .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.
分享
微博
QQ
微信
回复
2024-10-17 16:07:51
相关问题
鸿蒙UI框架没有 C++的 UI框架
14269浏览 • 2回复 待解决
HarmonyOS UI测试框架问题
685浏览 • 1回复 待解决
HarmonyOS应用开发 没有 C++ UI 框架
14777浏览 • 2回复 待解决
JS UI框架中canvas如何动态指定宽高
7483浏览 • 1回复 待解决
JS UI框架中FA和PA的page之间如何通信?
3333浏览 • 1回复 待解决
鸿蒙有哪些支持的第三方UI框架吗?
4372浏览 • 1回复 待解决
HarmonyOS 框架搭建
667浏览 • 1回复 待解决
HarmonyOS 开发基础框架
758浏览 • 1回复 待解决
HarmonyOS 网络框架demo
452浏览 • 1回复 待解决
HarmonyOS lottie框架问题
443浏览 • 1回复 待解决
HarmonyOS Navigation转场?
333浏览 • 0回复 待解决
HarmonyOS Navigation组件
657浏览 • 1回复 待解决