1# @system.router (Page Routing) 2 3The **Router** module provides APIs to access pages through URIs. 4 5> **NOTE** 6> 7> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.router`](js-apis-router.md) instead. 8> 9> 10> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11 12 13## Modules to Import 14 15 16```ts 17import router from '@system.router'; 18``` 19 20## router.push 21 22push(options: RouterOptions): void 23 24Navigates to a specified page in the application. 25 26**System capability**: SystemCapability.ArkUI.ArkUI.Full 27 28**Parameters** 29 30| Name | Type | Mandatory | Description | 31| ------- | ------------------------------- | ---- | -------------------------- | 32| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters. For details, see **RouterOptions**.| 33 34**Example** 35 36```ts 37// Current page 38import router from '@system.router'; 39class A{ 40 pushPage() { 41 router.push({ 42 uri: 'pages/routerpage2/routerpage2', 43 params: { 44 data1: 'message', 45 data2: { 46 data3: [123, 456, 789] 47 } 48 } 49 }); 50 } 51} 52export default new A() 53``` 54 55 56```ts 57// routerpage2 page 58class B{ 59 data:Record<string,string> = {'data1': 'default'} 60 data2:Record<string,number[]> = {'data3': [1, 2, 3]} 61 onInit() { 62 console.info('showData1:' + this.data.data1); 63 console.info('showData3:' + this.data2.data3); 64 } 65} 66export default new B() 67``` 68 69> **NOTE**<br> 70> The page routing stack supports a maximum of 32 pages. 71 72 73## router.replace 74 75replace(options: RouterOptions): void 76 77Replaces the current page with another one in the application and destroys the current page. 78 79**System capability**: SystemCapability.ArkUI.ArkUI.Lite 80 81**Parameters** 82 83| Name | Type | Mandatory | Description | 84| ------- | ------------------------------- | ---- | -------------------------- | 85| options | [RouterOptions](#routeroptions) | Yes | Page routing parameters. For details, see **RouterOptions**.| 86 87**Example** 88 89```ts 90// Current page 91import router from '@system.router'; 92class C{ 93 replacePage() { 94 router.replace({ 95 uri: 'pages/detail/detail', 96 params: { 97 data1: 'message' 98 } 99 }); 100 } 101} 102export default new C() 103``` 104 105 106```ts 107// detail page 108class Area { 109 data:Record<string,string> = {'data1': 'default'} 110 onInit() { 111 console.info('showData1:' + this.data) 112 } 113} 114export default new Area() 115``` 116 117## router.back 118 119back(options?: BackRouterOptions): void 120 121Returns to the previous page or a specified page. 122 123**System capability**: SystemCapability.ArkUI.ArkUI.Full 124 125**Parameters** 126 127| Name | Type | Mandatory | Description | 128| ------- | --------------------------------------- | ---- | ----------------------- | 129| options | [BackRouterOptions](#backrouteroptions) | No | For details, see **BackRouterOptions**.| 130 131**Example** 132 133```ts 134// index page 135import router from '@system.router'; 136class D{ 137 indexPushPage() { 138 router.push({ 139 uri: 'pages/detail/detail' 140 }); 141 } 142} 143export default new D() 144``` 145 146 147```ts 148// detail page 149import router from '@system.router'; 150class E{ 151 detailPushPage() { 152 router.push({ 153 uri: 'pages/mall/mall' 154 }); 155 } 156} 157export default new E() 158``` 159 160 161```ts 162// Navigate from the mall page to the detail page through router.back(). 163import router from '@system.router'; 164class F{ 165 mallBackPage() { 166 router.back(); 167 } 168} 169export default new F() 170``` 171 172 173```ts 174// Navigate from the detail page to the index page through router.back(). 175import router from '@system.router'; 176class G{ 177 defaultBack() { 178 router.back(); 179 } 180} 181export default new G() 182``` 183 184 185```ts 186// Return to the detail page through router.back(). 187import router from '@system.router'; 188class H{ 189 backToDetail() { 190 router.back({uri:'pages/detail/detail'}); 191 } 192} 193export default new H() 194``` 195 196> **NOTE**<br> 197> In the example, the **uri** field indicates the page route, which is specified by the **pages** list in the **config.json** file. 198 199## router.getParams<sup>7+</sup> 200 201getParams(): ParamsInterface 202 203Obtains parameter information about the current page. 204 205**System capability**: SystemCapability.ArkUI.ArkUI.Full 206 207**Return value** 208 209| Type | Description | 210| ----------------------------------- | --------------------- | 211| [ParamsInterface](#paramsinterface) | For details, see **ParamsInterface**.| 212 213## router.clear 214 215clear(): void 216 217Clears all historical pages in the stack and retains only the current page at the top of the stack. 218 219**System capability**: SystemCapability.ArkUI.ArkUI.Full 220 221**Example** 222 223```ts 224import router from '@system.router'; 225class I{ 226 clearPage() { 227 router.clear(); 228 } 229} 230export default new I() 231``` 232 233## router.getLength 234 235getLength(): string 236 237Obtains the number of pages in the current stack. 238 239**System capability**: SystemCapability.ArkUI.ArkUI.Full 240 241**Return value** 242 243| Type | Description | 244| ------ | ------------------ | 245| string | Number of pages in the stack. The maximum value is **32**.| 246 247**Example** 248 249```ts 250import router from '@system.router'; 251class J{ 252 getLength() { 253 let size = router.getLength(); 254 console.log('pages stack size = ' + size); 255 } 256} 257export default new J() 258``` 259 260## router.getState 261 262getState(): RouterState 263 264Obtains state information about the current page. 265 266**System capability**: SystemCapability.ArkUI.ArkUI.Full 267 268**Return value** 269 270| Type | Description | 271| --------------------------- | ----------------- | 272| [RouterState](#routerstate) | For details, see **RouterState**.| 273 274**Example** 275 276```ts 277import router from '@system.router'; 278class K{ 279 getState() { 280 let page = router.getState(); 281 console.log('current index = ' + page.index); 282 console.log('current name = ' + page.name); 283 console.log('current path = ' + page.path); 284 } 285} 286export default new K() 287``` 288 289## router.enableAlertBeforeBackPage<sup>6+</sup> 290 291enableAlertBeforeBackPage(options: EnableAlertBeforeBackPageOptions): void 292 293Enables the display of a confirm dialog box before returning to the previous page. 294 295**System capability**: SystemCapability.ArkUI.ArkUI.Full 296 297**Parameters** 298 299| Name | Type | Mandatory | Description | 300| ------- | ---------------------------------------- | ---- | -------------------------------------- | 301| options | [EnableAlertBeforeBackPageOptions](#enablealertbeforebackpageoptions6) | Yes | For details, see **EnableAlertBeforeBackPageOptions**.| 302 303**Example** 304 305```ts 306import router from '@system.router'; 307class L{ 308 enableAlertBeforeBackPage() { 309 router.enableAlertBeforeBackPage({ 310 message: 'Message Info', 311 success: ()=> { 312 console.log('success'); 313 }, 314 cancel: ()=> { 315 console.log('cancel'); 316 } 317 }); 318 } 319} 320export default new L() 321``` 322 323## router.disableAlertBeforeBackPage<sup>6+</sup> 324 325disableAlertBeforeBackPage(options?: DisableAlertBeforeBackPageOptions): void 326 327Disables the display of a confirm dialog box before returning to the previous page. 328 329**System capability**: SystemCapability.ArkUI.ArkUI.Full 330 331**Parameters** 332 333| Name | Type | Mandatory | Description | 334| ------- | ---------------------------------------- | ---- | --------------------------------------- | 335| options | [DisableAlertBeforeBackPageOptions](#disablealertbeforebackpageoptions6) | No | For details, see **DisableAlertBeforeBackPageOptions**.| 336 337**Example** 338 339```ts 340import router from '@system.router'; 341class Z{ 342 disableAlertBeforeBackPage() { 343 router.disableAlertBeforeBackPage({ 344 success: ()=> { 345 console.log('success'); 346 }, 347 cancel: ()=> { 348 console.log('cancel'); 349 } 350 }); 351 } 352} 353export default new Z() 354``` 355 356## RouterOptions 357 358Defines the page routing parameters. 359 360**System capability**: SystemCapability.ArkUI.ArkUI.Lite 361 362| Name | Type| Mandatory| Description | 363| ------ | -------- | ---- | ------------------------------------------------------------ | 364| uri | string | Yes | URI of the target page, in either of the following formats:<br>1. Absolute path, which is provided by the **pages** list in the **config.json** file. Example:<br>- pages/index/index<br> - pages/detail/detail<br>2. Specific path. If the URI is a slash (/), the home page is displayed.| 365| 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.| 366 367 368## BackRouterOptions 369 370Defines the parameters for routing back. 371 372**System capability**: The items in the table below require different system capabilities. For details, see the table. 373 374| Name | Type| Mandatory| Description | 375| ------ | -------- | ---- | ------------------------------------------------------------ | 376| uri<sup>7+</sup> | string | No | URI of the page to return to. If the specified page does not exist in the page stack, the application does not respond. If this parameter is not set, the application returns to the previous page.<br>**System capability**: SystemCapability.ArkUI.ArkUI.Full| 377| params<sup>7+</sup> | object | No | Data that needs to be passed to the target page during redirection.<br>**System capability**: SystemCapability.ArkUI.ArkUI.Lite| 378 379## RouterState 380 381Defines the page state. 382 383**System capability**: SystemCapability.ArkUI.ArkUI.Full 384 385| Name | Type | Mandatory | Description | 386| ----- | ------ | ---- | ---------------------------------- | 387| 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.| 388| name | string | Yes | Name of the current page, that is, the file name. | 389| path | string | Yes | Path of the current page. | 390 391## EnableAlertBeforeBackPageOptions<sup>6+</sup> 392 393Defines the **EnableAlertBeforeBackPage** parameters. 394 395**System capability**: SystemCapability.ArkUI.ArkUI.Full 396 397| Name | Type | Mandatory| Description | 398| -------- | ------------------------ | ---- | -------------------------------------------------- | 399| message | string | Yes | Content displayed in the confirm dialog box. | 400| success | (errMsg: string) => void | No | Called when the **OK** button in the confirm dialog box is clicked. **errMsg** indicates the returned information.| 401| cancel | (errMsg: string) => void | No | Called when the **Cancel** button in the confirm dialog box is clicked. **errMsg** indicates the returned information.| 402| complete | () => void | No | Called when the dialog box is closed. | 403 404## DisableAlertBeforeBackPageOptions<sup>6+</sup> 405 406Defines the **DisableAlertBeforeBackPage** parameters. 407 408**System capability**: SystemCapability.ArkUI.ArkUI.Full 409 410| Name | Type | Mandatory| Description | 411| -------- | ------------------------ | ---- | -------------------------------------------------- | 412| success | (errMsg: string) => void | No | Called when the dialog box is closed. **errMsg** indicates the returned information.| 413| cancel | (errMsg: string) => void | No | Called when the dialog box fails to be closed. **errMsg** indicates the returned information.| 414| complete | () => void | No | Called when the dialog box is closed. | 415 416## ParamsInterface 417 418| Name | Type| Description | 419| ------------- | -------- | -------------- | 420| [key: string] | object | List of routing parameters.| 421