1# NavPushPathHelper 2 3当跳转的目标[NavDestination](ts-basic-components-navdestination.md)在不同的hsp分包,且未被主包依赖,首次运行元服务只会下载安装主包,需要使用NavPushPathHelper先下载安装相应hsp分包,再将指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈。使[Navigation](ts-basic-components-navigation.md)支持动态加载hsp分包后再跳转。 4 5> **说明:** 6> 7> 该组件从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9## 导入模块 10 11```ts 12import { NavPushPathHelper } from '@kit.ArkUI' 13``` 14 15## 子组件 16 17无 18 19## 属性 20 21不支持[通用属性](ts-universal-attributes-size.md) 22 23## NavPushPathHelper 24 25对Navigation路由栈[NavPathStack](ts-basic-components-navigation.md#navpathstack10)的所有路由跳转接口进行了封装,在NavPushPathHelper中持有一个NavPathStack对象,在封装的跳转接口中,去判断子包是否存在,如果不存在则进行动态下载子包,等结果返回后调用NavPathStack的相应的接口将指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈。使用示例参见[示例](#示例)。 26 27### constructor 28 29constructor(navPathStack: NavPathStack) 30 31NavPushPathHelper的构造函数。 32 33**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 34 35**系统能力:** SystemCapability.ArkUI.ArkUI.Full 36 37**参数:** 38 39| 参数名 | 类型 | 必填 | 说明 | 40| ---- | ----------------------------- | ---- | -------------------- | 41| navPathStack | [NavPathStack](ts-basic-components-navigation.md#navpathstack10) | 是 | [Navigation](ts-basic-components-navigation.md)路由栈。 | 42 43### pushPath 44 45pushPath(moduleName: string, info: NavPathInfo, animated?: boolean): Promise\<void\> 46 47先判断分包是否存在,若不存在,则通过moduleName下载分包,再将info指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,使用Promise异步回调返回接口调用结果。 48 49**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 50 51**系统能力:** SystemCapability.ArkUI.ArkUI.Full 52 53**参数:** 54 55| 参数名 | 类型 | 必填 | 说明 | 56| ---- | ----------------------------- | ---- | -------------------- | 57| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 58| info | [NavPathInfo](ts-basic-components-navigation.md#navpathinfo10) | 是 | [NavDestination](ts-basic-components-navdestination.md)页面的信息。 | 59| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 60 61**返回值:** 62 63| 类型 | 说明 | 64| ------------------- | --------- | 65| Promise\<void\> | 异常返回结果。 | 66 67**错误码:** 68 69以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 70 71| 错误码ID | 错误信息 | 72| --------- | ------- | 73| 300001 | hsp silent install fail.| 74 75### pushPath 76 77pushPath(moduleName: string, info: NavPathInfo, options?: NavigationOptions): Promise\<void\> 78 79先判断分包是否存在,若不存在,则通过moduleName下载分包,再将info指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,使用Promise异步回调返回接口调用结果,具体根据options中指定不同的[LaunchMode](ts-basic-components-navigation.md#launchmode12枚举说明),有不同的行为。 80 81**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 82 83**系统能力:** SystemCapability.ArkUI.ArkUI.Full 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| ---- | ----------------------------- | ---- | -------------------- | 89| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 90| info | [NavPathInfo](ts-basic-components-navigation.md#navpathinfo10) | 是 | [NavDestination](ts-basic-components-navdestination.md)页面的信息。 | 91| options | [NavigationOptions](ts-basic-components-navigation.md#navigationoptions12) | 否 | 页面栈操作选项。 | 92 93**返回值:** 94 95| 类型 | 说明 | 96| ------------------- | --------- | 97| Promise\<void\> | 异常返回结果。 | 98 99**错误码:** 100 101以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 102 103| 错误码ID | 错误信息 | 104| --------- | ------- | 105| 300001 | hsp silent install fail.| 106 107### pushPathByName 108 109pushPathByName(moduleName: string, name: string, param: Object, animated?: boolean): Promise\<void\> 110 111先判断分包是否存在,若不存在,则通过moduleName下载分包,再将name指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,传递的数据为param,使用Promise异步回调返回接口调用结果。 112 113**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 114 115**系统能力:** SystemCapability.ArkUI.ArkUI.Full 116 117**参数:** 118 119| 参数名 | 类型 | 必填 | 说明 | 120| ----- | ------- | ---- | --------------------- | 121| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 122| name | string | 是 | [NavDestination](ts-basic-components-navdestination.md)页面名称。 | 123| param | Object | 是 | [NavDestination](ts-basic-components-navdestination.md)页面详细参数。 | 124| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 125 126**返回值:** 127 128| 类型 | 说明 | 129| ------------------- | --------- | 130| Promise\<void\> | 异常返回结果。 | 131 132**错误码:** 133 134以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 135 136| 错误码ID | 错误信息 | 137| --------- | ------- | 138| 300001 | hsp silent install fail.| 139 140### pushPathByName 141 142pushPathByName(moduleName: string, name: string, param: Object, onPop: Callback\<PopInfo>, animated?: boolean): Promise\<void\> 143 144先判断分包是否存在,若不存在,则通过moduleName下载分包,再将name指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,传递的数据为param,添加onPop回调接收入栈页面出栈时的返回结果,并进行处理,使用Promise异步回调返回接口调用结果。 145 146**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 147 148**系统能力:** SystemCapability.ArkUI.ArkUI.Full 149 150**参数:** 151 152| 参数名 | 类型 | 必填 | 说明 | 153|------|------|------|------| 154| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 155| name | string | 是 | [NavDestination](ts-basic-components-navdestination.md)页面名称。 | 156| param | Object | 是 | [NavDestination](ts-basic-components-navdestination.md)页面详细参数。 | 157| onPop | Callback\<[PopInfo](ts-basic-components-navigation.md#popinfo11)> | 是 | Callback回调,用于页面出栈时触发该回调处理返回结果。 | 158| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 159 160**返回值:** 161 162| 类型 | 说明 | 163| ------------------- | --------- | 164| Promise\<void\> | 异常返回结果。 | 165 166**错误码:** 167 168以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 169 170| 错误码ID | 错误信息 | 171| --------- | ------- | 172| 300001 | hsp silent install fail.| 173 174### pushDestination 175 176pushDestination(moduleName: string, info: NavPathInfo, animated?: boolean): Promise\<void\> 177 178先判断分包是否存在,若不存在,则通过moduleName下载分包,再将info指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,使用Promise异步回调返回接口调用结果。 179 180**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 181 182**系统能力:** SystemCapability.ArkUI.ArkUI.Full 183 184**参数:** 185 186| 参数名 | 类型 | 必填 | 说明 | 187| ---- | ----------------------------- | ---- | -------------------- | 188| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 189| info | [NavPathInfo](ts-basic-components-navigation.md#navpathinfo10) | 是 | [NavDestination](ts-basic-components-navdestination.md)页面的信息。 | 190| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 191 192**返回值:** 193 194| 类型 | 说明 | 195| ------------------- | --------- | 196| Promise\<void\> | 异常返回结果。 | 197 198**错误码:** 199 200以下错误码的详细介绍请参见[通用错误码](../../errorcode-universal.md)和[ohos.router(页面路由)](../errorcode-router.md)错误码。 201 202| 错误码ID | 错误信息 | 203| --------- | ------- | 204| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameters types. 3. Parameter verification failed. | 205| 100001 | Internal error.| 206| 100005 | Builder function not registered. | 207| 100006 | NavDestination not found.| 208| 300001 | hsp silent install fail.| 209 210### pushDestination 211 212pushDestination(moduleName: string, info: NavPathInfo, options?: NavigationOptions): Promise\<void\> 213 214先判断分包是否存在,若不存在,则通过moduleName下载分包,再将info指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,使用Promise异步回调返回接口调用结果,具体根据options中指定不同的[LaunchMode](ts-basic-components-navigation.md#launchmode12枚举说明),有不同的行为。 215 216**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 217 218**系统能力:** SystemCapability.ArkUI.ArkUI.Full 219 220**参数:** 221 222| 参数名 | 类型 | 必填 | 说明 | 223| ---- | ----------------------------- | ---- | -------------------- | 224| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 225| info | [NavPathInfo](ts-basic-components-navigation.md#navpathinfo10) | 是 | [NavDestination](ts-basic-components-navdestination.md)页面的信息。 | 226| options | [NavigationOptions](ts-basic-components-navigation.md#navigationoptions12) | 否 | 页面栈操作选项。 | 227 228**返回值:** 229 230| 类型 | 说明 | 231| ------------------- | --------- | 232| Promise\<void\> | 异常返回结果。 | 233 234**错误码:** 235 236以下错误码的详细介绍请参见[通用错误码](../../errorcode-universal.md)和[ohos.router(页面路由)](../errorcode-router.md)错误码。 237 238| 错误码ID | 错误信息 | 239| --------- | ------- | 240| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameters types. 3. Parameter verification failed. | 241| 100001 | Internal error.| 242| 100005 | Builder function not registered. | 243| 100006 | NavDestination not found.| 244| 300001 | hsp silent install fail.| 245 246### pushDestinationByName 247 248pushDestinationByName(moduleName: string, name: string, param: Object, animated?: boolean): Promise\<void\> 249 250先判断分包是否存在,若不存在,则通过moduleName下载分包,再将name指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,传递的数据为param,使用Promise异步回调返回接口调用结果。 251 252**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 253 254**系统能力:** SystemCapability.ArkUI.ArkUI.Full 255 256**参数:** 257 258| 参数名 | 类型 | 必填 | 说明 | 259| ----- | ------- | ---- | --------------------- | 260| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 261| name | string | 是 | [NavDestination](ts-basic-components-navdestination.md)页面名称。 | 262| param | Object | 是 | [NavDestination](ts-basic-components-navdestination.md)页面详细参数。 | 263| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 264 265**返回值:** 266 267| 类型 | 说明 | 268| ------------------- | --------- | 269| Promise\<void\> | 异常返回结果。 | 270 271**错误码:** 272 273以下错误码的详细介绍请参见[通用错误码](../../errorcode-universal.md)和[ohos.router(页面路由)](../errorcode-router.md)错误码。 274 275| 错误码ID | 错误信息 | 276| --------- | ------- | 277| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameters types. 3. Parameter verification failed. | 278| 100001 | Internal error.| 279| 100005 | Builder function not registered. | 280| 100006 | NavDestination not found.| 281| 300001 | hsp silent install fail.| 282 283### pushDestinationByName 284 285pushDestinationByName(moduleName: string, name: string, param: Object, onPop: Callback\<PopInfo>, animated?: boolean): Promise\<void\> 286 287先判断分包是否存在,若不存在,则通过moduleName下载分包,再将name指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,传递的数据为param,并且添加用于页面出栈时处理返回结果的OnPop回调,使用Promise异步回调返回接口调用结果。 288 289**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 290 291**系统能力:** SystemCapability.ArkUI.ArkUI.Full 292 293**参数:** 294 295| 参数名 | 类型 | 必填 | 说明 | 296| ----- | ------- | ---- | --------------------- | 297| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 298| name | string | 是 | [NavDestination](ts-basic-components-navdestination.md)页面名称。 | 299| param | Object | 是 | [NavDestination](ts-basic-components-navdestination.md)页面详细参数。 | 300| onPop | Callback\<[PopInfo](ts-basic-components-navigation.md#popinfo11)> | 是 | Callback回调,用于页面出栈时处理返回结果。 | 301| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 302 303**返回值:** 304 305| 类型 | 说明 | 306| ------------------- | --------- | 307| Promise\<void\> | 异常返回结果。 | 308 309**错误码:** 310 311以下错误码的详细介绍请参见[通用错误码](../../errorcode-universal.md)和[ohos.router(页面路由)](../errorcode-router.md)错误码。 312 313| 错误码ID | 错误信息 | 314| --------- | ------- | 315| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameters types. 3. Parameter verification failed. | 316| 100001 | Internal error.| 317| 100005 | Builder function not registered. | 318| 100006 | NavDestination not found.| 319| 300001 | hsp silent install fail.| 320 321### replacePath 322 323replacePath(moduleName: string, info: NavPathInfo, animated?: boolean): Promise\<void\> 324 325先判断分包是否存在,若不存在,则通过moduleName下载分包,再将当前页面栈栈顶退出,将info指定的[NavDestination](ts-basic-components-navdestination.md)页面信息入栈,使用Promise异步回调返回接口调用结果。 326 327**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 328 329**系统能力:** SystemCapability.ArkUI.ArkUI.Full 330 331**参数:** 332 333| 参数名 | 类型 | 必填 | 说明 | 334| ---- | ----------------------------- | ---- | -------------------- | 335| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 336| info | [NavPathInfo](ts-basic-components-navigation.md#navpathinfo10) | 是 | 新栈顶页面参数信息 | 337| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 338 339**返回值:** 340 341| 类型 | 说明 | 342| ------------------- | --------- | 343| Promise\<void\> | 异常返回结果。 | 344 345**错误码:** 346 347以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 348 349| 错误码ID | 错误信息 | 350| --------- | ------- | 351| 300001 | hsp silent install fail.| 352 353### replacePath 354 355replacePath(moduleName: string, info: NavPathInfo, options?: NavigationOptions): Promise\<void\> 356 357先判断分包是否存在,若不存在,则通过moduleName下载分包,再将当前页面栈栈顶退出,使用Promise异步回调返回接口调用结果,具体根据options中指定不同的[LaunchMode](ts-basic-components-navigation.md#launchmode12枚举说明),有不同的行为。 358 359**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 360 361**系统能力:** SystemCapability.ArkUI.ArkUI.Full 362 363**参数:** 364 365| 参数名 | 类型 | 必填 | 说明 | 366| ---- | ----------------------------- | ---- | -------------------- | 367| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 368| info | [NavPathInfo](ts-basic-components-navigation.md#navpathinfo10) | 是 | 新栈顶页面参数信息。 | 369| options | [NavigationOptions](ts-basic-components-navigation.md#navigationoptions12) | 否 | 页面栈操作选项。 | 370 371**返回值:** 372 373| 类型 | 说明 | 374| ------------------- | --------- | 375| Promise\<void\> | 异常返回结果。 | 376 377**错误码:** 378 379以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 380 381| 错误码ID | 错误信息 | 382| --------- | ------- | 383| 300001 | hsp silent install fail.| 384 385### replacePathByName 386 387replacePathByName(moduleName: string, name: string, param: Object, animated?: boolean): Promise\<void\> 388 389先判断分包是否存在,若不存在,则通过moduleName下载分包,再将当前页面栈栈顶退出,将name指定的页面入栈,使用Promise异步回调返回接口调用结果。 390 391**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 392 393**系统能力:** SystemCapability.ArkUI.ArkUI.Full 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| ----- | ------- | ---- | --------------------- | 399| moduleName | string | 是 | 目标[NavDestination](ts-basic-components-navdestination.md)所在分包的moduleName。 | 400| name | string | 是 | [NavDestination](ts-basic-components-navdestination.md)页面名称。 | 401| param | Object | 是 | [NavDestination](ts-basic-components-navdestination.md)页面详细参数。 | 402| animated | boolean | 否 | 是否支持转场动画,默认值:true。 | 403 404**返回值:** 405 406| 类型 | 说明 | 407| ------------------- | --------- | 408| Promise\<void\> | 异常返回结果。 | 409 410**错误码:** 411 412以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcode-router.md)错误码。 413 414| 错误码ID | 错误信息 | 415| --------- | ------- | 416| 300001 | hsp silent install fail.| 417 418## 事件 419 420不支持[通用事件](ts-universal-events-click.md) 421 422## 示例 423 424主包: 425```ts 426// Index.ets 427import { NavPushPathHelper } from '@kit.ArkUI' 428import { BusinessError } from '@kit.BasicServicesKit'; 429@Entry 430@Component 431struct NavigationExample { 432 pageInfo: NavPathStack = new NavPathStack() 433 helper: NavPushPathHelper = new NavPushPathHelper(this.pageInfo) 434 435 build() { 436 Navigation(this.pageInfo) { 437 Column() { 438 Button('StartTest', { stateEffect: true, type: ButtonType.Capsule }) 439 .width('80%') 440 .height(40) 441 .margin(20) 442 .onClick(() => { 443 this.helper.pushPath('hsptest1', { name: 'pageOne' }, false) 444 .catch((error: BusinessError) => { 445 console.error(`[pushPath]failed, error code = ${error.code}, error.message = ${error.message}.`); 446 }).then(() => { 447 console.error('[pushPath]success.'); 448 }); // 将name指定的NavDestination页面信息入栈。 449 }) 450 } 451 }.title('NavIndex') 452 } 453} 454``` 455分包hsptest1: 456```ts 457// PageOne.ets 458import { NavPushPathHelper } from '@kit.ArkUI' 459import { BusinessError } from '@kit.BasicServicesKit'; 460 461class TmpClass { 462 count: number = 10 463} 464 465class ParamWithOp { 466 operation: number = 1 467 count: number = 10 468} 469 470@Builder 471export function PageOneBuilder(name: string, param: Object) { 472 PageOne() 473} 474 475@Component 476export struct PageOne { 477 pageInfo: NavPathStack = new NavPathStack(); 478 helper: NavPushPathHelper = new NavPushPathHelper(this.pageInfo) 479 @State message: string = 'Hello World' 480 481 build() { 482 NavDestination() { 483 Column() { 484 Text(this.message) 485 .width('80%') 486 .height(50) 487 .margin(10) 488 489 Button('pushPath', { stateEffect: true, type: ButtonType.Capsule }) 490 .width('80%') 491 .height(35) 492 .margin(10) 493 .onClick(() => { 494 this.helper.pushPath('hsptest2', { name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => { 495 this.message = '[pushPath]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 496 }}).catch((error: BusinessError) => { 497 console.error(`[pushPath]failed, error code = ${error.code}, error.message = ${error.message}.`); 498 }).then(() => { 499 console.log('[pushPath]success.'); 500 }); 501 }) 502 503 Button('pushPath with NavigationOptions', { stateEffect: true, type: ButtonType.Capsule }) 504 .width('80%') 505 .height(35) 506 .margin(10) 507 .onClick(() => { 508 this.helper.pushPath('hsptest2', { name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => { 509 this.message = '[pushPath with NavigationOptions]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 510 }}, {launchMode:0, animated:true}).catch((error: BusinessError) => { 511 console.error(`[pushPath with NavigationOptions]failed, error code = ${error.code}, error.message = ${error.message}.`); 512 }).then(() => { 513 console.log('[pushPath with NavigationOptions]success.'); 514 }); 515 }) 516 517 Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule }) 518 .width('80%') 519 .height(35) 520 .margin(10) 521 .onClick(() => { 522 let tmp = new TmpClass() 523 this.helper.pushPathByName('hsptest2', 'pageTwo', tmp, (popInfo) => { 524 this.message = '[pushPathByName]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 525 }).catch((error: BusinessError) => { 526 console.error(`[pushPathByName]failed, error code = ${error.code}, error.message = ${error.message}.`); 527 }).then(() => { 528 console.log('[pushPathByName]success.'); 529 }); 530 }) 531 532 Button('pushPathByNameWithoutOnPop', { stateEffect: true, type: ButtonType.Capsule }) 533 .width('80%') 534 .height(35) 535 .margin(10) 536 .onClick(() => { 537 let tmp = new TmpClass() 538 this.helper.pushPathByName('hsptest2', 'pageTwo', tmp, true) 539 .catch((error: BusinessError) => { 540 console.error(`[pushPathByNameWithoutOnPop]failed, error code = ${error.code}, error.message = ${error.message}.`); 541 }).then(() => { 542 console.log('[pushPathByNameWithoutOnPop]success.'); 543 }); 544 }) 545 546 Button('pushDestination', { stateEffect: true, type: ButtonType.Capsule }) 547 .width('80%') 548 .height(35) 549 .margin(10) 550 .onClick(() => { 551 let tmp = new TmpClass() 552 this.helper.pushDestination('hsptest2', { name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => { 553 this.message = '[pushDestination]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 554 }}).catch((error: BusinessError) => { 555 console.error(`[pushDestination]failed, error code = ${error.code}, error.message = ${error.message}.`); 556 }).then(() => { 557 console.error('[pushDestination]success.'); 558 }); 559 }) 560 561 Button('pushDestination with NavigationOptions', { stateEffect: true, type: ButtonType.Capsule }) 562 .width('80%') 563 .height(35) 564 .margin(10) 565 .onClick(() => { 566 let tmp = new TmpClass() 567 this.helper.pushDestination('hsptest2', { name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => { 568 this.message = '[pushDestination with NavigationOptions]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 569 }}, {launchMode:0, animated:true}).catch((error: BusinessError) => { 570 console.error(`[pushDestination with NavigationOptions]failed, error code = ${error.code}, error.message = ${error.message}.`); 571 }).then(() => { 572 console.error('[pushDestination with NavigationOptions]success.'); 573 }); 574 }) 575 576 Button('pushDestinationByName', { stateEffect: true, type: ButtonType.Capsule }) 577 .width('80%') 578 .height(35) 579 .margin(10) 580 .onClick(() => { 581 let tmp = new TmpClass() 582 this.helper.pushDestinationByName('hsptest2','pageTwo', tmp, (popInfo) => { 583 this.message = '[pushDestinationByName]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 584 }).catch((error: BusinessError) => { 585 console.error(`[pushDestinationByName]failed, error code = ${error.code}, error.message = ${error.message}.`); 586 }).then(() => { 587 console.error('[pushDestinationByName]success.'); 588 }); 589 }) 590 591 Button('pushDestinationByNameWithoutOnPop', { stateEffect: true, type: ButtonType.Capsule }) 592 .width('80%') 593 .height(35) 594 .margin(10) 595 .onClick(() => { 596 let tmp = new TmpClass() 597 this.helper.pushDestinationByName('hsptest2','pageTwo', tmp, true) 598 .catch((error: BusinessError) => { 599 console.error(`[pushDestinationByNameWithoutOnPop]failed, error code = ${error.code}, error.message = ${error.message}.`); 600 }).then(() => { 601 console.error('[pushDestinationByNameWithoutOnPop]success.'); 602 }); 603 }) 604 605 Button('replacePath', { stateEffect: true, type: ButtonType.Capsule }) 606 .width('80%') 607 .height(35) 608 .margin(10) 609 .onClick(() => { 610 this.helper.replacePath('hsptest2', { name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => { 611 this.message = '[replacePath]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 612 }}).catch((error: BusinessError) => { 613 console.error(`[replacePath]failed, error code = ${error.code}, error.message = ${error.message}.`); 614 }).then(() => { 615 console.log('[replacePath]success.'); 616 }); 617 }) 618 619 Button('replacePath with NavigationOptions', { stateEffect: true, type: ButtonType.Capsule }) 620 .width('80%') 621 .height(35) 622 .margin(10) 623 .onClick(() => { 624 this.helper.replacePath('hsptest2', { name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => { 625 this.message = '[replacePath with NavigationOptions]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); 626 }}, {launchMode:0, animated:true}).catch((error: BusinessError) => { 627 console.error(`[replacePath with NavigationOptions]failed, error code = ${error.code}, error.message = ${error.message}.`); 628 }).then(() => { 629 console.log('[replacePath with NavigationOptions]success.'); 630 }); 631 }) 632 633 Button('replacePathByName', { stateEffect: true, type: ButtonType.Capsule }) 634 .width('80%') 635 .height(35) 636 .margin(10) 637 .onClick(() => { 638 let tmp = new TmpClass() 639 this.helper.replacePathByName('hsptest2', 'pageTwo', tmp) 640 .catch((error: BusinessError) => { 641 console.error(`[replacePathByName]failed, error code = ${error.code}, error.message = ${error.message}.`); 642 }).then(() => { 643 console.log('[replacePathByName]success.'); 644 }); 645 }) 646 647 }.width('100%').height('100%') 648 }.title('pageOne') 649 .onBackPressed(() => { 650 this.pageInfo.pop({ number: 1 }) // 弹出路由栈栈顶元素。 651 return true 652 }).onReady((context: NavDestinationContext) => { 653 this.pageInfo = context.pathStack; 654 this.helper = new NavPushPathHelper(this.pageInfo); 655 }) 656 } 657} 658``` 659 660```json 661// 工程配置文件module.json5中配置 {"routerMap": "$profile:route_map"} 662// route_map.json 663{ 664 "routerMap": [ 665 { 666 "name": "pageOne", 667 "pageSourceFile": "src/main/ets/pages/PageOne.ets", 668 "buildFunction": "PageOneBuilder", 669 "data": { 670 "description": "this is pageOne" 671 } 672 } 673 ] 674} 675``` 676 677分包hsptest2: 678```ts 679// PageTwo.ets 680 681class resultClass { 682 constructor(count: number) { 683 this.count = count; 684 } 685 count: number = 10 686} 687 688@Builder 689export function PageTwoBuilder() { 690 PageTwo() 691} 692 693@Component 694export struct PageTwo { 695 pathStack: NavPathStack = new NavPathStack() 696 697 build() { 698 NavDestination() { 699 Column() { 700 Button('pop', { stateEffect: true, type: ButtonType.Capsule }) 701 .width('80%') 702 .height(40) 703 .margin(20) 704 .onClick(() => { 705 this.pathStack.pop(new resultClass(1)); // 回退到上一个页面,将处理结果传入push的onPop回调中。 706 }) 707 }.width('100%').height('100%') 708 }.title('pageTwo') 709 .onBackPressed(() => { 710 this.pathStack.pop(new resultClass(0)); // 回退到上一个页面,将处理结果传入push的onPop回调。 711 return true; 712 }).onReady((context: NavDestinationContext) => { 713 this.pathStack = context.pathStack 714 }) 715 } 716} 717``` 718 719```json 720// 工程配置文件module.json5中配置 {"routerMap": "$profile:route_map"} 721// route_map.json 722{ 723 "routerMap": [ 724 { 725 "name": "pageTwo", 726 "pageSourceFile": "src/main/ets/pages/PageTwo.ets", 727 "buildFunction": "PageTwoBuilder", 728 "data": { 729 "description": "this is pageTwo" 730 } 731 } 732 ] 733} 734``` 735 736