中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
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') } }