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