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')  
  }  
}
分享
微博
QQ
微信
回复
2024-10-17 16:07:51
相关问题
鸿蒙UI框架没有 C++的 UI框架
13486浏览 • 2回复 待解决
HarmonyOS应用开发 没有 C++ UI 框架
14407浏览 • 2回复 待解决
JS UI框架中canvas如何动态指定宽高
6889浏览 • 1回复 待解决
JS UI框架中FA和PA的page之间如何通信?
2746浏览 • 1回复 待解决
鸿蒙有哪些支持的第三方UI框架吗?
3569浏览 • 1回复 待解决
HarmonyOS Navigation组件
257浏览 • 1回复 待解决
HarmonyOS Navigation转场?
121浏览 • 0回复 待解决
HarmonyOS 首页框架问题
327浏览 • 1回复 待解决
HarmonyOS 开发基础框架
302浏览 • 1回复 待解决
HarmonyOS Navigation相关资料
386浏览 • 1回复 待解决
HarmonyOS关于navigation问题
554浏览 • 1回复 待解决
HarmonyOS 消息首页框架实现
297浏览 • 1回复 待解决
HarmonyOS 数据库框架
357浏览 • 1回复 待解决
HarmonyOS 项目框架模块搭建
546浏览 • 1回复 待解决
HarmonyOS图片加载框架ImageKnife
399浏览 • 1回复 待解决
HarmonyOS Navigation 使用问题
420浏览 • 1回复 待解决
HarmonyOS Navigation使用问题
436浏览 • 1回复 待解决