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