js-apis-router.md 34.3 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.router (Page Routing)
W
wusongqing 已提交
2

E
ester.zhou 已提交
3 4
<!--subsystem: ArkUI-->

W
wusongqing 已提交
5 6
The **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 an application, and return to the previous page or a specified page.

W
wusongqing 已提交
7
> **NOTE**
W
wusongqing 已提交
8 9
>
> - 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.
W
wusongqing 已提交
10
>
W
wusongqing 已提交
11
> - 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.
E
ester.zhou 已提交
12 13 14 15
>
> - 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).
>
> - 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.
E
ester.zhou 已提交
16 17
>
> - 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).
W
wusongqing 已提交
18 19 20 21 22 23 24

## Modules to Import

```
import router from '@ohos.router'
```

25
## router.pushUrl<sup>9+</sup>
W
wusongqing 已提交
26

27
pushUrl(options: RouterOptions): Promise&lt;void&gt;
W
wusongqing 已提交
28 29 30 31 32 33

Navigates to a specified page in the application.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
34

W
wusongqing 已提交
35 36 37
| Name    | Type                             | Mandatory  | Description       |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters.|
W
wusongqing 已提交
38

39 40 41 42 43 44 45 46 47 48 49 50
**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
51 52 53
| 100001    | if UI execution context not found. |
| 100002    | if the uri is not exist. |
| 100003    | if the pages are pushed too much. |
W
wusongqing 已提交
54 55

**Example**
56

E
ester.zhou 已提交
57
```ts
E
ester.zhou 已提交
58 59 60 61 62 63
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
64
    }
E
ester.zhou 已提交
65 66 67 68 69 70 71
  }
})
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
72
  })
W
wusongqing 已提交
73
```
W
wusongqing 已提交
74

75 76 77 78 79 80 81 82 83 84 85 86 87
## router.pushUrl<sup>9+</sup>

pushUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void

Navigates to a specified page in the application.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description       |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters.|
E
ester.zhou 已提交
88
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |
89 90 91 92 93 94 95

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
96 97 98
| 100001    | if UI execution context not found. |
| 100002    | if the uri is not exist. |
| 100003    | if the pages are pushed too much. |
99 100 101

**Example**

E
ester.zhou 已提交
102
```ts
E
ester.zhou 已提交
103 104 105 106 107 108
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
109
    }
E
ester.zhou 已提交
110
  }
E
ester.zhou 已提交
111 112
}, (err) => {
  if (err) {
E
ester.zhou 已提交
113
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
E
ester.zhou 已提交
114 115 116 117
    return;
  }
  console.info('pushUrl success');
})
118 119 120 121
```
## router.pushUrl<sup>9+</sup>

pushUrl(options: RouterOptions, mode: RouterMode): Promise&lt;void&gt;
W
wusongqing 已提交
122 123 124 125 126 127

Navigates to a specified page in the application.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
128

W
wusongqing 已提交
129 130 131 132
| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
W
wusongqing 已提交
133

134 135 136 137 138 139 140 141 142 143 144 145
**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
146 147 148
| 100001    | if UI execution context not found. |
| 100002    | if the uri is not exist. |
| 100003    | if the pages are pushed too much. |
W
wusongqing 已提交
149 150

**Example**
151

E
ester.zhou 已提交
152
```ts
E
ester.zhou 已提交
153 154 155 156 157 158
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
159
    }
E
ester.zhou 已提交
160 161 162 163 164 165 166 167
  }
}, router.RouterMode.Standard)
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  })
W
wusongqing 已提交
168
```
W
wusongqing 已提交
169

170
## router.pushUrl<sup>9+</sup>
W
wusongqing 已提交
171

172
pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
173

174
Navigates to a specified page in the application.
W
wusongqing 已提交
175 176 177 178

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
W
wusongqing 已提交
179

180 181 182 183
| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
E
ester.zhou 已提交
184
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |
185 186 187 188 189 190 191

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
192 193 194
| 100001    | if UI execution context not found. |
| 100002    | if the uri is not exist. |
| 100003    | if the pages are pushed too much. |
195 196 197

**Example**

E
ester.zhou 已提交
198
```ts
E
ester.zhou 已提交
199 200 201 202 203 204
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
205
    }
E
ester.zhou 已提交
206 207 208 209 210 211 212 213
  }
}, router.RouterMode.Standard, (err) => {
  if (err) {
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('pushUrl success');
})
214 215 216 217 218 219 220 221 222 223 224 225
```

## router.replaceUrl<sup>9+</sup>

replaceUrl(options: RouterOptions): Promise&lt;void&gt;

Replaces the current page with another one in the application and destroys the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Lite

**Parameters**

W
wusongqing 已提交
226 227 228
| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | Yes  | Description of the new page.|
W
wusongqing 已提交
229

230 231 232 233 234 235 236 237 238 239 240 241
**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
242 243
| 100001    | if UI execution context not found, only throw in standard system. |
| 200002    | if the uri is not exist. |
244

W
wusongqing 已提交
245
**Example**
W
wusongqing 已提交
246

E
ester.zhou 已提交
247
```ts
E
ester.zhou 已提交
248 249 250 251 252 253 254 255 256 257 258
router.replaceUrl({
  url: 'pages/detail',
  params: {
    data1: 'message'
  }
})
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
259
  })
W
wusongqing 已提交
260
```
W
wusongqing 已提交
261

262
## router.replaceUrl<sup>9+</sup>
W
wusongqing 已提交
263

264
replaceUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
265 266 267

Replaces the current page with another one in the application and destroys the current page.

268 269 270 271 272 273 274
**System capability**: SystemCapability.ArkUI.ArkUI.Lite

**Parameters**

| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | Yes  | Description of the new page.|
E
ester.zhou 已提交
275
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |
276 277 278 279 280 281 282

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
283 284
| 100001    | if UI execution context not found, only throw in standard system. |
| 200002    | if the uri is not exist. |
285 286 287

**Example**

E
ester.zhou 已提交
288
```ts
E
ester.zhou 已提交
289 290 291 292 293 294 295 296 297 298 299 300
router.replaceUrl({
  url: 'pages/detail',
  params: {
    data1: 'message'
  }
}, (err) => {
  if (err) {
    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('replaceUrl success');
})
301 302 303 304 305 306 307 308 309
```

## router.replaceUrl<sup>9+</sup>

replaceUrl(options: RouterOptions, mode: RouterMode): Promise&lt;void&gt;

Replaces the current page with another one in the application and destroys the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Lite
W
wusongqing 已提交
310 311

**Parameters**
312

W
wusongqing 已提交
313 314 315 316
| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | Yes   | Description of the new page. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
W
wusongqing 已提交
317

318 319 320 321 322 323 324 325 326 327 328 329 330

**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
331
| 100001    | if can not get the delegate, only throw in standard system. |
E
ester.zhou 已提交
332
| 200002    | if the uri is not exist. |
333

W
wusongqing 已提交
334 335
**Example**

E
ester.zhou 已提交
336
```ts
E
ester.zhou 已提交
337 338 339 340 341 342 343 344 345 346 347 348
router.replaceUrl({
  url: 'pages/detail',
  params: {
    data1: 'message'
  }
}, router.RouterMode.Standard)
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
  })
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
```

## router.replaceUrl<sup>9+</sup>

replaceUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void

Replaces the current page with another one in the application and destroys the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Lite

**Parameters**

| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | Yes   | Description of the new page. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
E
ester.zhou 已提交
365
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |
366 367 368 369 370 371 372

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
373
| 100001    | if UI execution context not found, only throw in standard system. |
E
ester.zhou 已提交
374
| 200002    | if the uri is not exist. |
375 376 377

**Example**

E
ester.zhou 已提交
378
```ts
E
ester.zhou 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391
router.replaceUrl({
  url: 'pages/detail',
  params: {
    data1: 'message'
  }
}, router.RouterMode.Standard, (err) => {
  if (err) {
    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('replaceUrl success');
});

W
wusongqing 已提交
392
```
W
wusongqing 已提交
393

E
ester.zhou 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
## router.pushNamedRoute<sup>10+</sup>

pushNamedRoute(options: NamedRouterOptions): Promise&lt;void&gt;

Navigates to a page using the named route. This API uses a promise to return the result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description       |
| ------- | ------------------------------- | ---- | --------- |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes   | Page routing parameters.|

**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
| 100001    | if UI execution context not found. |
| 100003    | if the pages are pushed too much. |
| 100004    | if the named route is not exist. |

**Example**

```ts
E
ester.zhou 已提交
427 428 429 430 431 432
router.pushNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
433
    }
E
ester.zhou 已提交
434 435 436 437 438 439 440
  }
})
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`);
E
ester.zhou 已提交
441 442 443
  })
```

E
ester.zhou 已提交
444 445
For details, see [UI Development-Named Route](../../ui/arkts-routing.md#named-route).

E
ester.zhou 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
## router.pushNamedRoute<sup>10+</sup>

pushNamedRoute(options: NamedRouterOptions, callback: AsyncCallback&lt;void&gt;): void

Navigates to a page using the named route. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description       |
| ------- | ------------------------------- | ---- | --------- |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes   | Page routing parameters.|
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
| 100001    | if UI execution context not found. |
| 100003    | if the pages are pushed too much. |
| 100004    | if the named route is not exist. |

**Example**

```ts
router.pushNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
    }
  }
}, (err) => {
  if (err) {
    console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('pushNamedRoute success');
})
```
## router.pushNamedRoute<sup>10+</sup>

pushNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise&lt;void&gt;

Navigates to a page using the named route. This API uses a promise to return the result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes   | Page routing parameters. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|

**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
| 100001    | if UI execution context not found. |
| 100003    | if the pages are pushed too much. |
| 100004    | if the named route is not exist. |

**Example**

```ts
E
ester.zhou 已提交
524 525 526 527 528 529
router.pushNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
530
    }
E
ester.zhou 已提交
531 532 533 534 535 536 537 538
  }
}, router.RouterMode.Standard)
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`);
  })
E
ester.zhou 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
```

## router.pushNamedRoute<sup>10+</sup>

pushNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void

Navigates to a page using the named route. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes   | Page routing parameters. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
| 100001    | if UI execution context not found. |
| 100003    | if the pages are pushed too much. |
| 100004    | if the named route is not exist. |

**Example**

```ts
router.pushNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
    }
  }
}, router.RouterMode.Standard, (err) => {
  if (err) {
    console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('pushNamedRoute success');
})
```

## router.replaceNamedRoute<sup>10+</sup>

replaceNamedRoute(options: NamedRouterOptions): Promise&lt;void&gt;

Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes  | Description of the new page.|

**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
613
| 100001    | if UI execution context not found, only throw in standard system. |
E
ester.zhou 已提交
614 615 616 617 618
| 100004    | if the named route is not exist. |

**Example**

```ts
E
ester.zhou 已提交
619 620 621 622 623 624 625 626 627 628 629
router.replaceNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message'
  }
})
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`);
E
ester.zhou 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  })
```

## router.replaceNamedRoute<sup>10+</sup>

replaceNamedRoute(options: NamedRouterOptions, callback: AsyncCallback&lt;void&gt;): void

Replaces 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.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes  | Description of the new page.|
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
654
| 100001    | if UI execution context not found, only throw in standard system. |
E
ester.zhou 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
| 100004    | if the named route is not exist. |

**Example**

```ts
router.replaceNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message'
  }
}, (err) => {
  if (err) {
    console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('replaceNamedRoute success');
})
```

## router.replaceNamedRoute<sup>10+</sup>

replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise&lt;void&gt;

Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes   | Description of the new page. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|


**Return value**

| Type               | Description       |
| ------------------- | --------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
702
| 100001    | if can not get the delegate, only throw in standard system. |
E
ester.zhou 已提交
703 704 705 706 707
| 100004    | if the named route is not exist. |

**Example**

```ts
E
ester.zhou 已提交
708 709 710 711 712 713 714 715 716 717 718 719
router.replaceNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message'
  }
}, router.RouterMode.Standard)
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`);
  })
E
ester.zhou 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
```

## router.replaceNamedRoute<sup>10+</sup>

replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void

Replaces 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.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [NamedRouterOptions](#namedrouteroptions10) | Yes   | Description of the new page. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
| callback | AsyncCallback&lt;void&gt;      | Yes  | Callback used to return the result.  |

**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
744
| 100001    | if UI execution context not found, only throw in standard system. |
E
ester.zhou 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
| 100004    | if the named route is not exist. |

**Example**

```ts
router.replaceNamedRoute({
  name: 'myPage',
  params: {
    data1: 'message'
  }
}, router.RouterMode.Standard, (err) => {
  if (err) {
    console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('replaceNamedRoute success');
});

```

W
wusongqing 已提交
765 766 767 768 769 770 771 772 773
## router.back

back(options?: RouterOptions ): void

Returns to the previous page or a specified page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
774

W
wusongqing 已提交
775 776
| Name | Type                           | Mandatory| Description                                                        |
| ------- | ------------------------------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
777
| 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.|
W
wusongqing 已提交
778 779 780

**Example**

E
ester.zhou 已提交
781
```ts
W
wusongqing 已提交
782 783
router.back({url:'pages/detail'});    
```
W
wusongqing 已提交
784 785 786 787 788 789 790 791 792 793

## router.clear

clear(): void

Clears all historical pages in the stack and retains only the current page at the top of the stack.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
W
wusongqing 已提交
794

E
ester.zhou 已提交
795
```ts
W
wusongqing 已提交
796
router.clear();    
W
wusongqing 已提交
797
```
W
wusongqing 已提交
798 799 800 801 802 803 804

## router.getLength

getLength(): string

Obtains the number of pages in the current stack.

W
wusongqing 已提交
805 806
**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
807
**Return value**
808

W
wusongqing 已提交
809 810
| Type    | Description                |
| ------ | ------------------ |
W
wusongqing 已提交
811
| string | Number of pages in the stack. The maximum value is **32**.|
W
wusongqing 已提交
812 813

**Example**
814

E
ester.zhou 已提交
815
```ts
E
ester.zhou 已提交
816
let size = router.getLength();        
W
wusongqing 已提交
817
console.log('pages stack size = ' + size);    
W
wusongqing 已提交
818
```
W
wusongqing 已提交
819 820 821 822 823 824 825 826 827 828 829

## router.getState

getState(): RouterState

Obtains state information about the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Return value**

W
wusongqing 已提交
830 831
| Type                         | Description     |
| --------------------------- | ------- |
W
wusongqing 已提交
832
| [RouterState](#routerstate) | Page routing state.|
G
Gloria 已提交
833

W
wusongqing 已提交
834 835
**Example**

E
ester.zhou 已提交
836
```ts
E
ester.zhou 已提交
837
let page = router.getState();
W
wusongqing 已提交
838 839 840 841 842
console.log('current index = ' + page.index);
console.log('current name = ' + page.name);
console.log('current path = ' + page.path);
```

W
wusongqing 已提交
843
## RouterState
W
wusongqing 已提交
844

W
wusongqing 已提交
845 846 847 848
Describes the page routing state.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
849 850 851 852 853
| Name | Type  | Mandatory| Description                                                        |
| ----- | ------ | ---- | ------------------------------------------------------------ |
| 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.|
| name  | string | No  | Name of the current page, that is, the file name.                          |
| path  | string | Yes  | Path of the current page.                                        |
W
wusongqing 已提交
854

E
ester.zhou 已提交
855
## router.showAlertBeforeBackPage<sup>9+</sup>
W
wusongqing 已提交
856

E
ester.zhou 已提交
857
showAlertBeforeBackPage(options: EnableAlertOptions): void
W
wusongqing 已提交
858 859 860 861 862 863

Enables the display of a confirm dialog box before returning to the previous page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
864

W
wusongqing 已提交
865 866 867
| Name    | Type                                      | Mandatory  | Description       |
| ------- | ---------------------------------------- | ---- | --------- |
| options | [EnableAlertOptions](#enablealertoptions) | Yes   | Description of the dialog box.|
W
wusongqing 已提交
868

869 870 871 872 873 874
**Error codes**

For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md).

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
875
| 100001    | if UI execution context not found. |
876

W
wusongqing 已提交
877 878
**Example**

E
ester.zhou 已提交
879
```ts
E
ester.zhou 已提交
880 881 882 883 884 885 886 887 888
router.showAlertBeforeBackPage({
  message: 'Message Info'
})
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`showAlertBeforeBackPage failed, code is ${error.code}, message is ${error.message}`);
  })
E
ester.zhou 已提交
889
```
W
wusongqing 已提交
890 891 892 893
## EnableAlertOptions

Describes the confirm dialog box.

W
wusongqing 已提交
894
**System capability**: SystemCapability.ArkUI.ArkUI.Full
W
wusongqing 已提交
895

W
wusongqing 已提交
896 897 898
| Name     | Type    | Mandatory  | Description      |
| ------- | ------ | ---- | -------- |
| message | string | Yes   | Content displayed in the confirm dialog box.|
W
wusongqing 已提交
899

E
ester.zhou 已提交
900
## router.hideAlertBeforeBackPage<sup>9+</sup>
W
wusongqing 已提交
901

E
ester.zhou 已提交
902
hideAlertBeforeBackPage(): void
W
wusongqing 已提交
903 904 905 906 907 908

Disables the display of a confirm dialog box before returning to the previous page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
909

E
ester.zhou 已提交
910
```ts
E
ester.zhou 已提交
911
router.hideAlertBeforeBackPage();    
W
wusongqing 已提交
912
```
W
wusongqing 已提交
913 914 915 916 917 918 919 920 921 922 923

##  router.getParams

getParams(): Object

Obtains the parameters passed from the page that initiates redirection to the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Return value**

E
ester.zhou 已提交
924 925 926
| Type  | Description                              |
| ------ | ---------------------------------- |
| object | Parameters passed from the page that initiates redirection to the current page.|
W
wusongqing 已提交
927 928 929

**Example**

W
wusongqing 已提交
930 931 932 933 934 935 936 937
```
router.getParams();
```

## RouterOptions

Describes the page routing options.

W
wusongqing 已提交
938
**System capability**: SystemCapability.ArkUI.ArkUI.Lite
W
wusongqing 已提交
939

940 941 942
| Name  | Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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.|
E
ester.zhou 已提交
943
| 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.|
W
wusongqing 已提交
944 945 946 947 948 949 950 951 952 953 954


  > **NOTE**
  > The page routing stack supports a maximum of 32 pages.

## RouterMode<sup>9+</sup>

Enumerates the routing modes.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
955 956
| Name    | Description                                                        |
| -------- | ------------------------------------------------------------ |
E
ester.zhou 已提交
957 958
| 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.|
| 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.|
W
wusongqing 已提交
959

E
ester.zhou 已提交
960 961 962 963 964 965 966 967 968 969 970
## NamedRouterOptions<sup>10+</sup>

Describes the named route options.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

| Name  | Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| name   | string | Yes  | Name of the target named route.|
| 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.|

W
wusongqing 已提交
971 972 973 974
## Examples

### JavaScript-based Web-like Development Paradigm

E
ester.zhou 已提交
975
```ts
W
wusongqing 已提交
976 977 978 979 980 981
// Current page
export default {
  pushPage() {
    router.push({
      url: 'pages/detail/detail',
      params: {
E
ester.zhou 已提交
982 983
        data1: 'message'
      }
W
wusongqing 已提交
984
    });
W
wusongqing 已提交
985
  }
W
wusongqing 已提交
986 987
}
```
E
ester.zhou 已提交
988
```ts
W
wusongqing 已提交
989 990 991
// detail page
export default {
  onInit() {
E
ester.zhou 已提交
992
    console.info('showData1:' + router.getParams()['data1']);
W
wusongqing 已提交
993
  }
W
wusongqing 已提交
994 995
}
```
W
wusongqing 已提交
996

W
wusongqing 已提交
997
### TypeScript-based Declarative Development Paradigm
W
wusongqing 已提交
998

W
wusongqing 已提交
999
```ts
E
ester.zhou 已提交
1000
// Navigate to the target page through router.pushUrl with the params parameter carried.
W
wusongqing 已提交
1001 1002 1003 1004 1005
import router from '@ohos.router'

@Entry
@Component
struct Index {
E
ester.zhou 已提交
1006
  async routePage() {
W
wusongqing 已提交
1007 1008 1009 1010 1011 1012
    let options = {
      url: 'pages/second',
      params: {
        text: 'This is the value on the first page.',
        data: {
          array: [12, 45, 78]
E
ester.zhou 已提交
1013
        }
W
wusongqing 已提交
1014 1015
      }
    }
W
wusongqing 已提交
1016
    try {
E
ester.zhou 已提交
1017
      await router.pushUrl(options)
W
wusongqing 已提交
1018 1019
    } catch (err) {
      console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
W
wusongqing 已提交
1020 1021 1022
    }
  }

W
wusongqing 已提交
1023 1024
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
E
ester.zhou 已提交
1025 1026 1027
      Text('This is the first page.')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
W
wusongqing 已提交
1028 1029 1030 1031 1032
      Button() {
        Text('next page')
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
      }.type(ButtonType.Capsule)
E
ester.zhou 已提交
1033 1034 1035 1036
      .margin({ top: 20 })
      .backgroundColor('#ccc')
      .onClick(() => {
        this.routePage()
W
wusongqing 已提交
1037
      })
W
wusongqing 已提交
1038
    }
W
wusongqing 已提交
1039 1040
    .width('100%')
    .height('100%')
W
wusongqing 已提交
1041
  }
W
wusongqing 已提交
1042 1043
}
```
W
wusongqing 已提交
1044

W
wusongqing 已提交
1045 1046 1047
```ts
// Receive the transferred parameters on the second page.
import router from '@ohos.router'
W
wusongqing 已提交
1048

W
wusongqing 已提交
1049 1050 1051 1052 1053
@Entry
@Component
struct Second {
  private content: string = "This is the second page."
  @State text: string = router.getParams()['text']
E
ester.zhou 已提交
1054
  @State data: object = router.getParams()['data']
E
ester.zhou 已提交
1055 1056
  @State secondData: string = ''

W
wusongqing 已提交
1057 1058 1059 1060 1061 1062 1063
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text(`${this.content}`)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
      Text(this.text)
        .fontSize(30)
E
ester.zhou 已提交
1064
        .onClick(() => {
E
ester.zhou 已提交
1065
          this.secondData = (this.data['array'][1]).toString()
W
wusongqing 已提交
1066
        })
E
ester.zhou 已提交
1067
        .margin({ top: 20 })
E
ester.zhou 已提交
1068
      Text(`This is the data passed from the first page: ${this.secondData}`)
W
wusongqing 已提交
1069
        .fontSize(20)
E
ester.zhou 已提交
1070 1071
        .margin({ top: 20 })
        .backgroundColor('red')
W
wusongqing 已提交
1072 1073 1074 1075 1076 1077
    }
    .width('100%')
    .height('100%')
  }
}
```
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

## router.push<sup>(deprecated)</sup>

push(options: RouterOptions): void

Navigates to a specified page in the application.

This API is deprecated since API version 9. You are advised to use [pushUrl<sup>9+</sup>](#routerpushurl9) instead.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                             | Mandatory  | Description       |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters.|


**Example**

E
ester.zhou 已提交
1098
```ts
1099 1100 1101 1102 1103 1104
router.push({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
1105 1106
    }
  }
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
});
```

## router.replace<sup>(deprecated)</sup>

replace(options: RouterOptions): void

Replaces the current page with another one in the application and destroys the current page.

This API is deprecated since API version 9. You are advised to use [replaceUrl<sup>9+</sup>](#routerreplaceurl9) instead.

E
ester.zhou 已提交
1118
**System capability**: SystemCapability.ArkUI.ArkUI.Lite
1119 1120 1121 1122 1123 1124 1125 1126 1127

**Parameters**

| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | Yes  | Description of the new page.|

**Example**

E
ester.zhou 已提交
1128
```ts
1129 1130 1131
router.replace({
  url: 'pages/detail',
  params: {
E
ester.zhou 已提交
1132 1133
    data1: 'message'
  }
1134 1135 1136 1137 1138 1139 1140 1141 1142
});
```

## router.enableAlertBeforeBackPage<sup>(deprecated)</sup>

enableAlertBeforeBackPage(options: EnableAlertOptions): void

Enables the display of a confirm dialog box before returning to the previous page.

E
ester.zhou 已提交
1143
This API is deprecated since API version 9. You are advised to use [showAlertBeforeBackPage<sup>9+</sup>](#routershowalertbeforebackpage9) instead.
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                                      | Mandatory  | Description       |
| ------- | ---------------------------------------- | ---- | --------- |
| options | [EnableAlertOptions](#enablealertoptions) | Yes   | Description of the dialog box.|

**Example**

E
ester.zhou 已提交
1155 1156 1157 1158 1159
```ts
router.enableAlertBeforeBackPage({
  message: 'Message Info'
});
```
E
ester.zhou 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172

## router.disableAlertBeforeBackPage<sup>(deprecated)</sup>

disableAlertBeforeBackPage(): void

Disables the display of a confirm dialog box before returning to the previous page.

This API is deprecated since API version 9. You are advised to use [hideAlertBeforeBackPage<sup>9+</sup>](#routerhidealertbeforebackpage9) instead.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**

E
ester.zhou 已提交
1173 1174
```ts
router.disableAlertBeforeBackPage();
E
ester.zhou 已提交
1175
```