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