1# @ohos.router (Page Routing) 2 3 4The **Router** module provides APIs to access pages through URLs. You can use the APIs to navigate to a specified page in an application, replace the current page with another one in the same application, and return to the previous page or a specified page. 5 6> **NOTE** 7> 8> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 9> 10> - Page routing APIs can be invoked only after page rendering is complete. Do not call these APIs in **onInit** and **onReady** when the page is still in the rendering phase. 11> 12> - The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](./js-apis-arkui-UIContext.md#uicontext). 13> 14> - Since API version 10, you can use the [getRouter](./js-apis-arkui-UIContext.md#getrouter) API in [UIContext](./js-apis-arkui-UIContext.md#uicontext) to obtain the [Router](./js-apis-arkui-UIContext.md#router) object associated with the current UI context. 15> 16> - To achieve a better transition effect, you are advised to use the [\<Navigation>](../../ui/arkts-navigation-navigation.md) component and [modal transition](../../ui/arkts-modal-transition.md). 17 18## Modules to Import 19 20``` 21import router from '@ohos.router' 22``` 23 24## router.pushUrl<sup>9+</sup> 25 26pushUrl(options: RouterOptions): Promise<void> 27 28Navigates to a specified page in the application. 29 30**System capability**: SystemCapability.ArkUI.ArkUI.Full 31 32**Parameters** 33 34| Name | Type | Mandatory | Description | 35| ------- | ------------------------------- | ---- | --------- | 36| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters.| 37 38**Return value** 39 40| Type | Description | 41| ------------------- | --------- | 42| Promise<void> | Promise used to return the result.| 43 44**Error codes** 45 46For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 47 48| ID | Error Message| 49| --------- | ------- | 50| 100001 | if UI execution context not found. | 51| 100002 | if the uri is not exist. | 52| 100003 | if the pages are pushed too much. | 53 54**Example** 55 56```ts 57import { BusinessError } from '@ohos.base'; 58 59class innerParams { 60 data3:number[] 61 62 constructor(tuple:number[]) { 63 this.data3 = tuple 64 } 65} 66 67class routerParams { 68 data1:string 69 data2:innerParams 70 71 constructor(str:string, tuple:number[]) { 72 this.data1 = str 73 this.data2 = new innerParams(tuple) 74 } 75} 76 77try { 78 router.pushUrl({ 79 url: 'pages/routerpage2', 80 params: new routerParams('message' ,[123,456,789]) 81 }) 82} catch (err) { 83 console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 84} 85``` 86 87## router.pushUrl<sup>9+</sup> 88 89pushUrl(options: RouterOptions, callback: AsyncCallback<void>): void 90 91Navigates to a specified page in the application. 92 93**System capability**: SystemCapability.ArkUI.ArkUI.Full 94 95**Parameters** 96 97| Name | Type | Mandatory | Description | 98| ------- | ------------------------------- | ---- | --------- | 99| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters.| 100| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 101 102**Error codes** 103 104For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 105 106| ID | Error Message| 107| --------- | ------- | 108| 100001 | if UI execution context not found. | 109| 100002 | if the uri is not exist. | 110| 100003 | if the pages are pushed too much. | 111 112**Example** 113 114```ts 115class innerParams { 116 data3:number[] 117 118 constructor(tuple:number[]) { 119 this.data3 = tuple 120 } 121} 122 123class routerParams { 124 data1:string 125 data2:innerParams 126 127 constructor(str:string, tuple:number[]) { 128 this.data1 = str 129 this.data2 = new innerParams(tuple) 130 } 131} 132 133router.pushUrl({ 134 url: 'pages/routerpage2', 135 params: new routerParams('message' ,[123,456,789]) 136}, (err) => { 137 if (err) { 138 console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 139 return; 140 } 141 console.info('pushUrl success'); 142}) 143``` 144## router.pushUrl<sup>9+</sup> 145 146pushUrl(options: RouterOptions, mode: RouterMode): Promise<void> 147 148Navigates to a specified page in the application. 149 150**System capability**: SystemCapability.ArkUI.ArkUI.Full 151 152**Parameters** 153 154| Name | Type | Mandatory | Description | 155| ------- | ------------------------------- | ---- | ---------- | 156| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters. | 157| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 158 159**Return value** 160 161| Type | Description | 162| ------------------- | --------- | 163| Promise<void> | Promise used to return the result.| 164 165**Error codes** 166 167For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 168 169| ID | Error Message| 170| --------- | ------- | 171| 100001 | if UI execution context not found. | 172| 100002 | if the uri is not exist. | 173| 100003 | if the pages are pushed too much. | 174 175**Example** 176 177```ts 178import { BusinessError } from '@ohos.base'; 179 180class innerParams { 181 data3:number[] 182 183 constructor(tuple:number[]) { 184 this.data3 = tuple 185 } 186} 187 188class routerParams { 189 data1:string 190 data2:innerParams 191 192 constructor(str:string, tuple:number[]) { 193 this.data1 = str 194 this.data2 = new innerParams(tuple) 195 } 196} 197 198try { 199 router.pushUrl({ 200 url: 'pages/routerpage2', 201 params: new routerParams('message' ,[123,456,789]) 202 }, router.RouterMode.Standard) 203} catch (err) { 204 console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 205} 206``` 207 208## router.pushUrl<sup>9+</sup> 209 210pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 211 212Navigates to a specified page in the application. 213 214**System capability**: SystemCapability.ArkUI.ArkUI.Full 215 216**Parameters** 217 218| Name | Type | Mandatory | Description | 219| ------- | ------------------------------- | ---- | ---------- | 220| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters. | 221| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 222| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 223 224**Error codes** 225 226For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 227 228| ID | Error Message| 229| --------- | ------- | 230| 100001 | if UI execution context not found. | 231| 100002 | if the uri is not exist. | 232| 100003 | if the pages are pushed too much. | 233 234**Example** 235 236```ts 237class innerParams { 238 data3:number[] 239 240 constructor(tuple:number[]) { 241 this.data3 = tuple 242 } 243} 244 245class routerParams { 246 data1:string 247 data2:innerParams 248 249 constructor(str:string, tuple:number[]) { 250 this.data1 = str 251 this.data2 = new innerParams(tuple) 252 } 253} 254 255router.pushUrl({ 256 url: 'pages/routerpage2', 257 params: new routerParams('message' ,[123,456,789]) 258}, router.RouterMode.Standard, (err) => { 259 if (err) { 260 console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 261 return; 262 } 263 console.info('pushUrl success'); 264}) 265``` 266 267## router.replaceUrl<sup>9+</sup> 268 269replaceUrl(options: RouterOptions): Promise<void> 270 271Replaces the current page with another one in the application and destroys the current page. This API cannot be used to configure page transition effects. To configure page transition effects, use the [\<Navigation>](../../ui/arkts-navigation-navigation.md) component. 272 273**System capability**: SystemCapability.ArkUI.ArkUI.Lite 274 275**Parameters** 276 277| Name | Type | Mandatory| Description | 278| ------- | ------------------------------- | ---- | ------------------ | 279| options | [RouterOptions](#routeroptions) | Yes | Description of the new page.| 280 281**Return value** 282 283| Type | Description | 284| ------------------- | --------- | 285| Promise<void> | Promise used to return the result.| 286 287**Error codes** 288 289For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 290 291| ID | Error Message| 292| --------- | ------- | 293| 100001 | if UI execution context not found, only throw in standard system. | 294| 200002 | if the uri is not exist. | 295 296**Example** 297 298```ts 299import { BusinessError } from '@ohos.base'; 300 301class routerParams { 302 data1:string 303 304 constructor(str:string) { 305 this.data1 = str 306 } 307} 308 309try { 310 router.replaceUrl({ 311 url: 'pages/detail', 312 params: new routerParams('message') 313 }) 314} catch (err) { 315 console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 316} 317``` 318 319## router.replaceUrl<sup>9+</sup> 320 321replaceUrl(options: RouterOptions, callback: AsyncCallback<void>): void 322 323Replaces the current page with another one in the application and destroys the current page. 324 325**System capability**: SystemCapability.ArkUI.ArkUI.Lite 326 327**Parameters** 328 329| Name | Type | Mandatory| Description | 330| ------- | ------------------------------- | ---- | ------------------ | 331| options | [RouterOptions](#routeroptions) | Yes | Description of the new page.| 332| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 333 334**Error codes** 335 336For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 337 338| ID | Error Message| 339| --------- | ------- | 340| 100001 | if UI execution context not found, only throw in standard system. | 341| 200002 | if the uri is not exist. | 342 343**Example** 344 345```ts 346class routerParams { 347 data1:string 348 349 constructor(str:string) { 350 this.data1 = str 351 } 352} 353 354router.replaceUrl({ 355 url: 'pages/detail', 356 params: new routerParams('message') 357}, (err) => { 358 if (err) { 359 console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`); 360 return; 361 } 362 console.info('replaceUrl success'); 363}) 364``` 365 366## router.replaceUrl<sup>9+</sup> 367 368replaceUrl(options: RouterOptions, mode: RouterMode): Promise<void> 369 370Replaces the current page with another one in the application and destroys the current page. 371 372**System capability**: SystemCapability.ArkUI.ArkUI.Lite 373 374**Parameters** 375 376| Name | Type | Mandatory | Description | 377| ------- | ------------------------------- | ---- | ---------- | 378| options | [RouterOptions](#routeroptions) | Yes | Description of the new page. | 379| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 380 381 382**Return value** 383 384| Type | Description | 385| ------------------- | --------- | 386| Promise<void> | Promise used to return the result.| 387 388**Error codes** 389 390For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 391 392| ID | Error Message| 393| --------- | ------- | 394| 100001 | if can not get the delegate, only throw in standard system. | 395| 200002 | if the uri is not exist. | 396 397**Example** 398 399```ts 400import { BusinessError } from '@ohos.base'; 401 402class routerParams { 403 data1:string 404 405 constructor(str:string) { 406 this.data1 = str 407 } 408} 409 410try { 411 router.replaceUrl({ 412 url: 'pages/detail', 413 params: new routerParams('message') 414 }, router.RouterMode.Standard) 415} catch (err) { 416 console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 417} 418``` 419 420## router.replaceUrl<sup>9+</sup> 421 422replaceUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 423 424Replaces the current page with another one in the application and destroys the current page. 425 426**System capability**: SystemCapability.ArkUI.ArkUI.Lite 427 428**Parameters** 429 430| Name | Type | Mandatory | Description | 431| ------- | ------------------------------- | ---- | ---------- | 432| options | [RouterOptions](#routeroptions) | Yes | Description of the new page. | 433| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 434| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 435 436**Error codes** 437 438For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 439 440| ID | Error Message| 441| --------- | ------- | 442| 100001 | if UI execution context not found, only throw in standard system. | 443| 200002 | if the uri is not exist. | 444 445**Example** 446 447```ts 448class routerParams { 449 data1:string 450 451 constructor(str:string) { 452 this.data1 = str 453 } 454} 455 456router.replaceUrl({ 457 url: 'pages/detail', 458 params: new routerParams('message') 459}, router.RouterMode.Standard, (err) => { 460 if (err) { 461 console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`); 462 return; 463 } 464 console.info('replaceUrl success'); 465}); 466 467``` 468 469## router.pushNamedRoute<sup>10+</sup> 470 471pushNamedRoute(options: NamedRouterOptions): Promise<void> 472 473Navigates to a page using the named route. This API uses a promise to return the result. 474 475**System capability**: SystemCapability.ArkUI.ArkUI.Full 476 477**Parameters** 478 479| Name | Type | Mandatory | Description | 480| ------- | ------------------------------- | ---- | --------- | 481| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Page routing parameters.| 482 483**Return value** 484 485| Type | Description | 486| ------------------- | --------- | 487| Promise<void> | Promise used to return the result.| 488 489**Error codes** 490 491For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 492 493| ID | Error Message| 494| --------- | ------- | 495| 100001 | if UI execution context not found. | 496| 100003 | if the pages are pushed too much. | 497| 100004 | if the named route is not exist. | 498 499**Example** 500 501```ts 502import { BusinessError } from '@ohos.base'; 503 504class innerParams { 505 data3:number[] 506 507 constructor(tuple:number[]) { 508 this.data3 = tuple 509 } 510} 511 512class routerParams { 513 data1:string 514 data2:innerParams 515 516 constructor(str:string, tuple:number[]) { 517 this.data1 = str 518 this.data2 = new innerParams(tuple) 519 } 520} 521 522try { 523 router.pushNamedRoute({ 524 name: 'myPage', 525 params: new routerParams('message' ,[123,456,789]) 526 }) 527} catch (err) { 528 console.error(`pushNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 529} 530``` 531 532For details, see [UI Development-Named Route](../../ui/arkts-routing.md#named-route). 533 534## router.pushNamedRoute<sup>10+</sup> 535 536pushNamedRoute(options: NamedRouterOptions, callback: AsyncCallback<void>): void 537 538Navigates to a page using the named route. This API uses an asynchronous callback to return the result. 539 540**System capability**: SystemCapability.ArkUI.ArkUI.Full 541 542**Parameters** 543 544| Name | Type | Mandatory | Description | 545| ------- | ------------------------------- | ---- | --------- | 546| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Page routing parameters.| 547| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 548 549**Error codes** 550 551For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 552 553| ID | Error Message| 554| --------- | ------- | 555| 100001 | if UI execution context not found. | 556| 100003 | if the pages are pushed too much. | 557| 100004 | if the named route is not exist. | 558 559**Example** 560 561```ts 562class innerParams { 563 data3:number[] 564 565 constructor(tuple:number[]) { 566 this.data3 = tuple 567 } 568} 569 570class routerParams { 571 data1:string 572 data2:innerParams 573 574 constructor(str:string, tuple:number[]) { 575 this.data1 = str 576 this.data2 = new innerParams(tuple) 577 } 578} 579 580router.pushNamedRoute({ 581 name: 'myPage', 582 params: new routerParams('message' ,[123,456,789]) 583}, (err) => { 584 if (err) { 585 console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`); 586 return; 587 } 588 console.info('pushNamedRoute success'); 589}) 590``` 591## router.pushNamedRoute<sup>10+</sup> 592 593pushNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise<void> 594 595Navigates to a page using the named route. This API uses a promise to return the result. 596 597**System capability**: SystemCapability.ArkUI.ArkUI.Full 598 599**Parameters** 600 601| Name | Type | Mandatory | Description | 602| ------- | ------------------------------- | ---- | ---------- | 603| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Page routing parameters. | 604| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 605 606**Return value** 607 608| Type | Description | 609| ------------------- | --------- | 610| Promise<void> | Promise used to return the result.| 611 612**Error codes** 613 614For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 615 616| ID | Error Message| 617| --------- | ------- | 618| 100001 | if UI execution context not found. | 619| 100003 | if the pages are pushed too much. | 620| 100004 | if the named route is not exist. | 621 622**Example** 623 624```ts 625import { BusinessError } from '@ohos.base'; 626 627class innerParams { 628 data3:number[] 629 630 constructor(tuple:number[]) { 631 this.data3 = tuple 632 } 633} 634 635class routerParams { 636 data1:string 637 data2:innerParams 638 639 constructor(str:string, tuple:number[]) { 640 this.data1 = str 641 this.data2 = new innerParams(tuple) 642 } 643} 644 645try { 646 router.pushNamedRoute({ 647 name: 'myPage', 648 params: new routerParams('message' ,[123,456,789]) 649 }, router.RouterMode.Standard) 650} catch (err) { 651 console.error(`pushNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 652} 653``` 654 655## router.pushNamedRoute<sup>10+</sup> 656 657pushNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 658 659Navigates to a page using the named route. This API uses an asynchronous callback to return the result. 660 661**System capability**: SystemCapability.ArkUI.ArkUI.Full 662 663**Parameters** 664 665| Name | Type | Mandatory | Description | 666| ------- | ------------------------------- | ---- | ---------- | 667| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Page routing parameters. | 668| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 669| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 670 671**Error codes** 672 673For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 674 675| ID | Error Message| 676| --------- | ------- | 677| 100001 | if UI execution context not found. | 678| 100003 | if the pages are pushed too much. | 679| 100004 | if the named route is not exist. | 680 681**Example** 682 683```ts 684class innerParams { 685 data3:number[] 686 687 constructor(tuple:number[]) { 688 this.data3 = tuple 689 } 690} 691 692class routerParams { 693 data1:string 694 data2:innerParams 695 696 constructor(str:string, tuple:number[]) { 697 this.data1 = str 698 this.data2 = new innerParams(tuple) 699 } 700} 701 702router.pushNamedRoute({ 703 name: 'myPage', 704 params: new routerParams('message' ,[123,456,789]) 705}, router.RouterMode.Standard, (err) => { 706 if (err) { 707 console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`); 708 return; 709 } 710 console.info('pushNamedRoute success'); 711}) 712``` 713 714## router.replaceNamedRoute<sup>10+</sup> 715 716replaceNamedRoute(options: NamedRouterOptions): Promise<void> 717 718Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result. 719 720**System capability**: SystemCapability.ArkUI.ArkUI.Full 721 722**Parameters** 723 724| Name | Type | Mandatory| Description | 725| ------- | ------------------------------- | ---- | ------------------ | 726| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Description of the new page.| 727 728**Return value** 729 730| Type | Description | 731| ------------------- | --------- | 732| Promise<void> | Promise used to return the result.| 733 734**Error codes** 735 736For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 737 738| ID | Error Message| 739| --------- | ------- | 740| 100001 | if UI execution context not found, only throw in standard system. | 741| 100004 | if the named route is not exist. | 742 743**Example** 744 745```ts 746import { BusinessError } from '@ohos.base'; 747 748class routerParams { 749 data1:string 750 751 constructor(str:string) { 752 this.data1 = str 753 } 754} 755 756try { 757 router.replaceNamedRoute({ 758 name: 'myPage', 759 params: new routerParams('message') 760 }) 761} catch (err) { 762 console.error(`replaceNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 763} 764``` 765 766## router.replaceNamedRoute<sup>10+</sup> 767 768replaceNamedRoute(options: NamedRouterOptions, callback: AsyncCallback<void>): void 769 770Replaces the current page with another one using the named route and destroys the current page. This API uses an asynchronous callback to return the result. 771 772**System capability**: SystemCapability.ArkUI.ArkUI.Full 773 774**Parameters** 775 776| Name | Type | Mandatory| Description | 777| ------- | ------------------------------- | ---- | ------------------ | 778| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Description of the new page.| 779| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 780 781**Error codes** 782 783For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 784 785| ID | Error Message| 786| --------- | ------- | 787| 100001 | if UI execution context not found, only throw in standard system. | 788| 100004 | if the named route is not exist. | 789 790**Example** 791 792```ts 793class routerParams { 794 data1:string 795 796 constructor(str:string) { 797 this.data1 = str 798 } 799} 800 801router.replaceNamedRoute({ 802 name: 'myPage', 803 params: new routerParams('message') 804}, (err) => { 805 if (err) { 806 console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`); 807 return; 808 } 809 console.info('replaceNamedRoute success'); 810}) 811``` 812 813## router.replaceNamedRoute<sup>10+</sup> 814 815replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise<void> 816 817Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result. 818 819**System capability**: SystemCapability.ArkUI.ArkUI.Full 820 821**Parameters** 822 823| Name | Type | Mandatory | Description | 824| ------- | ------------------------------- | ---- | ---------- | 825| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Description of the new page. | 826| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 827 828 829**Return value** 830 831| Type | Description | 832| ------------------- | --------- | 833| Promise<void> | Promise used to return the result.| 834 835**Error codes** 836 837For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 838 839| ID | Error Message| 840| --------- | ------- | 841| 100001 | if can not get the delegate, only throw in standard system. | 842| 100004 | if the named route is not exist. | 843 844**Example** 845 846```ts 847import { BusinessError } from '@ohos.base'; 848 849class routerParams { 850 data1:string 851 852 constructor(str:string) { 853 this.data1 = str 854 } 855} 856 857try { 858 router.replaceNamedRoute({ 859 name: 'myPage', 860 params: new routerParams('message') 861 }, router.RouterMode.Standard) 862} catch (err) { 863 console.error(`replaceNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 864} 865``` 866 867## router.replaceNamedRoute<sup>10+</sup> 868 869replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 870 871Replaces the current page with another one using the named route and destroys the current page. This API uses an asynchronous callback to return the result. 872 873**System capability**: SystemCapability.ArkUI.ArkUI.Full 874 875**Parameters** 876 877| Name | Type | Mandatory | Description | 878| ------- | ------------------------------- | ---- | ---------- | 879| options | [NamedRouterOptions](#namedrouteroptions10) | Yes | Description of the new page. | 880| mode | [RouterMode](#routermode9) | Yes | Routing mode.| 881| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 882 883**Error codes** 884 885For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 886 887| ID | Error Message| 888| --------- | ------- | 889| 100001 | if UI execution context not found, only throw in standard system. | 890| 100004 | if the named route is not exist. | 891 892**Example** 893 894```ts 895class routerParams { 896 data1:string 897 898 constructor(str:string) { 899 this.data1 = str 900 } 901} 902 903router.replaceNamedRoute({ 904 name: 'myPage', 905 params: new routerParams('message') 906}, router.RouterMode.Standard, (err) => { 907 if (err) { 908 console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`); 909 return; 910 } 911 console.info('replaceNamedRoute success'); 912}); 913 914``` 915 916## router.back 917 918back(options?: RouterOptions ): void 919 920Returns to the previous page or a specified page. 921 922**System capability**: SystemCapability.ArkUI.ArkUI.Full 923 924**Parameters** 925 926| Name | Type | Mandatory| Description | 927| ------- | ------------------------------- | ---- | ------------------------------------------------------------ | 928| options | [RouterOptions](#routeroptions) | No | Description of the page. The **url** parameter indicates the URL of the page to return to. If the specified page does not exist in the page stack, the application does not respond. If no URL is set, the application returns to the previous page, and the page is not rebuilt. The page in the page stack is not reclaimed. It will be reclaimed after being popped up.| 929 930**Example** 931 932```ts 933router.back({url:'pages/detail'}); 934``` 935 936## router.clear 937 938clear(): void 939 940Clears all historical pages in the stack and retains only the current page at the top of the stack. 941 942**System capability**: SystemCapability.ArkUI.ArkUI.Full 943 944**Example** 945 946```ts 947router.clear(); 948``` 949 950## router.getLength 951 952getLength(): string 953 954Obtains the number of pages in the current stack. 955 956**System capability**: SystemCapability.ArkUI.ArkUI.Full 957 958**Return value** 959 960| Type | Description | 961| ------ | ------------------ | 962| string | Number of pages in the stack. The maximum value is **32**.| 963 964**Example** 965 966```ts 967let size = router.getLength(); 968console.log('pages stack size = ' + size); 969``` 970 971## router.getState 972 973getState(): RouterState 974 975Obtains state information about the current page. 976 977**System capability**: SystemCapability.ArkUI.ArkUI.Full 978 979**Return value** 980 981| Type | Description | 982| --------------------------- | ------- | 983| [RouterState](#routerstate) | Page routing state.| 984 985**Example** 986 987```ts 988let page = router.getState(); 989console.log('current index = ' + page.index); 990console.log('current name = ' + page.name); 991console.log('current path = ' + page.path); 992``` 993 994## RouterState 995 996Describes the page routing state. 997 998**System capability**: SystemCapability.ArkUI.ArkUI.Full 999 1000| Name | Type | Mandatory| Description | 1001| ----- | ------ | ---- | ------------------------------------------------------------ | 1002| index | number | Yes | Index of the current page in the stack. The index starts from 1 from the bottom to the top of the stack.| 1003| name | string | No | Name of the current page, that is, the file name. | 1004| path | string | Yes | Path of the current page. | 1005 1006## router.showAlertBeforeBackPage<sup>9+</sup> 1007 1008showAlertBeforeBackPage(options: EnableAlertOptions): void 1009 1010Enables the display of a confirm dialog box before returning to the previous page. 1011 1012**System capability**: SystemCapability.ArkUI.ArkUI.Full 1013 1014**Parameters** 1015 1016| Name | Type | Mandatory | Description | 1017| ------- | ---------------------------------------- | ---- | --------- | 1018| options | [EnableAlertOptions](#enablealertoptions) | Yes | Description of the dialog box.| 1019 1020**Error codes** 1021 1022For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1023 1024| ID | Error Message| 1025| --------- | ------- | 1026| 100001 | if UI execution context not found. | 1027 1028**Example** 1029 1030```ts 1031import { BusinessError } from '@ohos.base'; 1032 1033try { 1034 router.showAlertBeforeBackPage({ 1035 message: 'Message Info' 1036 }); 1037} catch(err) { 1038 console.error(`showAlertBeforeBackPage failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 1039} 1040``` 1041## EnableAlertOptions 1042 1043Describes the confirm dialog box. 1044 1045**System capability**: SystemCapability.ArkUI.ArkUI.Full 1046 1047| Name | Type | Mandatory | Description | 1048| ------- | ------ | ---- | -------- | 1049| message | string | Yes | Content displayed in the confirm dialog box.| 1050 1051## router.hideAlertBeforeBackPage<sup>9+</sup> 1052 1053hideAlertBeforeBackPage(): void 1054 1055Disables the display of a confirm dialog box before returning to the previous page. 1056 1057**System capability**: SystemCapability.ArkUI.ArkUI.Full 1058 1059**Example** 1060 1061```ts 1062router.hideAlertBeforeBackPage(); 1063``` 1064 1065## router.getParams 1066 1067getParams(): Object 1068 1069Obtains the parameters passed from the page that initiates redirection to the current page. 1070 1071**System capability**: SystemCapability.ArkUI.ArkUI.Full 1072 1073**Return value** 1074 1075| Type | Description | 1076| ------ | ---------------------------------- | 1077| object | Parameters passed from the page that initiates redirection to the current page.| 1078 1079**Example** 1080 1081``` 1082router.getParams(); 1083``` 1084 1085## RouterOptions 1086 1087Describes the page routing options. 1088 1089**System capability**: SystemCapability.ArkUI.ArkUI.Lite 1090 1091| Name | Type | Mandatory| Description | 1092| ------ | ------ | ---- | ------------------------------------------------------------ | 1093| url | string | Yes | URL of the target page, in either of the following formats:<br>- Absolute path of the page. The value is available in the pages list in the **config.json** file, for example:<br>- pages/index/index<br>- pages/detail/detail<br>- Particular path. If the URL is a slash (/), the home page is displayed.| 1094| params | object | No | Data that needs to be passed to the target page during redirection. The received data becomes invalid when the page is switched to another page. The target page can use **router.getParams()** to obtain the passed parameters, for example, **this.keyValue** (**keyValue** is the value of a key in **params**). In the web-like paradigm, these parameters can be directly used on the target page. If the field specified by **key** already exists on the target page, the passed value of the key will be displayed.<br>**NOTE**<br>The **params** parameter cannot pass objects returned by methods and system APIs, for example, **PixelMap** objects defined and returned by media APIs. To pass such objects, extract from them the basic type attributes to be passed, and then construct objects of the object type.| 1095 1096 1097 > **NOTE** 1098 > The page routing stack supports a maximum of 32 pages. 1099 1100## RouterMode<sup>9+</sup> 1101 1102Enumerates the routing modes. 1103 1104**System capability**: SystemCapability.ArkUI.ArkUI.Full 1105 1106| Name | Description | 1107| -------- | ------------------------------------------------------------ | 1108| Standard | Multi-instance mode. It is the default routing mode.<br>The target page is added to the top of the page stack, regardless of whether a page with the same URL exists in the stack.<br>**NOTE**<br>If the routing mode is not used, the page is redirected to in multi-instance mode.| 1109| Single | Singleton mode.<br>If the URL of the target page already exists in the page stack, the page closest to the top of the stack is moved to the top and becomes a new page.<br>If the URL of the target page does not exist in the page stack, the page is redirected to in multi-instance mode.| 1110 1111## NamedRouterOptions<sup>10+</sup> 1112 1113Describes the named route options. 1114 1115**System capability**: SystemCapability.ArkUI.ArkUI.Full 1116 1117| Name | Type | Mandatory| Description | 1118| ------ | ------ | ---- | ------------------------------------------------------------ | 1119| name | string | Yes | Name of the target named route.| 1120| params | object | No | Data that needs to be passed to the target page during redirection. The target page can use **router.getParams()** to obtain the passed parameters, for example, **this.keyValue** (**keyValue** is the value of a key in **params**). In the web-like paradigm, these parameters can be directly used on the target page. If the field specified by **key** already exists on the target page, the passed value of the key will be displayed.| 1121 1122## Examples 1123 1124### JavaScript-based Web-like Development Paradigm 1125 1126The following sample code applies only to JavaScript files, not ArkTS files. 1127```js 1128// Current page 1129export default { 1130 pushPage() { 1131 router.push({ 1132 url: 'pages/detail/detail', 1133 params: { 1134 data1: 'message' 1135 } 1136 }); 1137 } 1138} 1139``` 1140```js 1141// detail page 1142export default { 1143 onInit() { 1144 console.info('showData1:' + router.getParams()['data1']); 1145 } 1146} 1147``` 1148 1149### TypeScript-based Declarative Development Paradigm 1150 1151```ts 1152// Navigate to the target page through router.pushUrl with the params parameter carried. 1153import router from '@ohos.router' 1154import { BusinessError } from '@ohos.base' 1155 1156// Define the class for passing parameters. 1157class innerParams { 1158 array:number[] 1159 1160 constructor(tuple:number[]) { 1161 this.array = tuple 1162 } 1163} 1164 1165class routerParams { 1166 text:string 1167 data:innerParams 1168 1169 constructor(str:string, tuple:number[]) { 1170 this.text = str 1171 this.data = new innerParams(tuple) 1172 } 1173} 1174 1175@Entry 1176@Component 1177struct Index { 1178 async routePage() { 1179 let options:router.RouterOptions = { 1180 url: 'pages/second', 1181 params: new routerParams ('This is the value on the first page', [12, 45, 78]) 1182 } 1183 try { 1184 await router.pushUrl(options) 1185 } catch (err) { 1186 console.info(` fail callback, code: ${(err as BusinessError).code}, msg: ${(err as BusinessError).message}`) 1187 } 1188 } 1189 1190 build() { 1191 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1192 Text('This is the first page.') 1193 .fontSize(50) 1194 .fontWeight(FontWeight.Bold) 1195 Button() { 1196 Text('next page') 1197 .fontSize(25) 1198 .fontWeight(FontWeight.Bold) 1199 }.type(ButtonType.Capsule) 1200 .margin({ top: 20 }) 1201 .backgroundColor('#ccc') 1202 .onClick(() => { 1203 this.routePage() 1204 }) 1205 } 1206 .width('100%') 1207 .height('100%') 1208 } 1209} 1210``` 1211 1212```ts 1213// Receive the transferred parameters on the second page. 1214import router from '@ohos.router' 1215 1216class innerParams { 1217 array:number[] 1218 1219 constructor(tuple:number[]) { 1220 this.array = tuple 1221 } 1222} 1223 1224class routerParams { 1225 text:string 1226 data:innerParams 1227 1228 constructor(str:string, tuple:number[]) { 1229 this.text = str 1230 this.data = new innerParams(tuple) 1231 } 1232} 1233 1234@Entry 1235@Component 1236struct Second { 1237 private content: string = "This is the second page." 1238 @State text: string = (router.getParams() as routerParams).text 1239 @State data: object = (router.getParams() as routerParams).data 1240 @State secondData: string = '' 1241 1242 build() { 1243 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1244 Text(`${this.content}`) 1245 .fontSize(50) 1246 .fontWeight(FontWeight.Bold) 1247 Text(this.text) 1248 .fontSize(30) 1249 .onClick(() => { 1250 this.secondData = (this.data['array'][1]).toString() 1251 }) 1252 .margin({ top: 20 }) 1253 Text(`This is the data passed from the first page: ${this.secondData}`) 1254 .fontSize(20) 1255 .margin({ top: 20 }) 1256 .backgroundColor('red') 1257 } 1258 .width('100%') 1259 .height('100%') 1260 } 1261} 1262``` 1263 1264## router.push<sup>(deprecated)</sup> 1265 1266push(options: RouterOptions): void 1267 1268Navigates to a specified page in the application. 1269 1270This API is deprecated since API version 9. You are advised to use [pushUrl<sup>9+</sup>](#routerpushurl9) instead. 1271 1272**System capability**: SystemCapability.ArkUI.ArkUI.Full 1273 1274**Parameters** 1275 1276| Name | Type | Mandatory | Description | 1277| ------- | ------------------------------- | ---- | --------- | 1278| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters.| 1279 1280 1281**Example** 1282 1283```ts 1284class innerParams { 1285 data3:number[] 1286 1287 constructor(tuple:number[]) { 1288 this.data3 = tuple 1289 } 1290} 1291 1292class routerParams { 1293 data1:string 1294 data2:innerParams 1295 1296 constructor(str:string, tuple:number[]) { 1297 this.data1 = str 1298 this.data2 = new innerParams(tuple) 1299 } 1300} 1301 1302router.push({ 1303 url: 'pages/routerpage2', 1304 params: new routerParams('message' ,[123,456,789]) 1305}); 1306``` 1307 1308## router.replace<sup>(deprecated)</sup> 1309 1310replace(options: RouterOptions): void 1311 1312Replaces the current page with another one in the application and destroys the current page. 1313 1314This API is deprecated since API version 9. You are advised to use [replaceUrl<sup>9+</sup>](#routerreplaceurl9) instead. 1315 1316**System capability**: SystemCapability.ArkUI.ArkUI.Lite 1317 1318**Parameters** 1319 1320| Name | Type | Mandatory| Description | 1321| ------- | ------------------------------- | ---- | ------------------ | 1322| options | [RouterOptions](#routeroptions) | Yes | Description of the new page.| 1323 1324**Example** 1325 1326```ts 1327class routerParams { 1328 data1:string 1329 1330 constructor(str:string) { 1331 this.data1 = str 1332 } 1333} 1334 1335router.replace({ 1336 url: 'pages/detail', 1337 params: new routerParams('message') 1338}); 1339``` 1340 1341## router.enableAlertBeforeBackPage<sup>(deprecated)</sup> 1342 1343enableAlertBeforeBackPage(options: EnableAlertOptions): void 1344 1345Enables the display of a confirm dialog box before returning to the previous page. 1346 1347This API is deprecated since API version 9. You are advised to use [showAlertBeforeBackPage<sup>9+</sup>](#routershowalertbeforebackpage9) instead. 1348 1349**System capability**: SystemCapability.ArkUI.ArkUI.Full 1350 1351**Parameters** 1352 1353| Name | Type | Mandatory | Description | 1354| ------- | ---------------------------------------- | ---- | --------- | 1355| options | [EnableAlertOptions](#enablealertoptions) | Yes | Description of the dialog box.| 1356 1357**Example** 1358 1359```ts 1360router.enableAlertBeforeBackPage({ 1361 message: 'Message Info' 1362}); 1363``` 1364 1365## router.disableAlertBeforeBackPage<sup>(deprecated)</sup> 1366 1367disableAlertBeforeBackPage(): void 1368 1369Disables the display of a confirm dialog box before returning to the previous page. 1370 1371This API is deprecated since API version 9. You are advised to use [hideAlertBeforeBackPage<sup>9+</sup>](#routerhidealertbeforebackpage9) instead. 1372 1373**System capability**: SystemCapability.ArkUI.ArkUI.Full 1374 1375**Example** 1376 1377```ts 1378router.disableAlertBeforeBackPage(); 1379``` 1380