js-apis-router.md 24.0 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.router
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4
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 已提交
5
> **NOTE**
W
wusongqing 已提交
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.
W
wusongqing 已提交
8
>
W
wusongqing 已提交
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.
W
wusongqing 已提交
10 11 12 13 14 15 16

## Modules to Import

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

17
## router.pushUrl<sup>9+</sup>
W
wusongqing 已提交
18

19
pushUrl(options: RouterOptions): Promise&lt;void&gt;
W
wusongqing 已提交
20 21 22 23 24 25

Navigates to a specified page in the application.

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

**Parameters**
26

W
wusongqing 已提交
27 28 29
| Name    | Type                             | Mandatory  | Description       |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters.|
W
wusongqing 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42
**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 已提交
43 44 45
| 100001    | If UI execution context not found. |
| 100002    | If the uri is not exist. |
| 100003    | If the pages are pushed too much. |
W
wusongqing 已提交
46 47

**Example**
48

W
wusongqing 已提交
49
```js
50 51 52 53 54 55 56
try {
  router.pushUrl({
    url: 'pages/routerpage2',
    params: {
      data1: 'message',
      data2: {
        data3: [123, 456, 789]
E
ester.zhou 已提交
57 58
      }
    }
59 60 61 62 63 64 65 66 67 68
  })
    .then(() => {
      // success
    })
    .catch(err => {
      console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
    })
} catch (error) {
  console.error(`pushUrl args error code is ${error.code}, message is ${error.message}`);
};
W
wusongqing 已提交
69
```
W
wusongqing 已提交
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
## 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.|
| 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 已提交
92 93 94
| 100001    | If UI execution context not found. |
| 100002    | If the uri is not exist. |
| 100003    | If the pages are pushed too much. |
95 96 97 98 99 100 101 102 103 104 105

**Example**

```js
try {
  router.pushUrl({
    url: 'pages/routerpage2',
    params: {
      data1: 'message',
      data2: {
        data3: [123, 456, 789]
E
ester.zhou 已提交
106 107
      }
    }
108 109 110 111 112 113 114 115 116 117 118 119 120 121
  }, (err) => {
    if (err) {
      console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    console.info('pushUrl success');
  });
} catch (error) {
  console.error(`pushUrl args error code is ${error.code}, message is ${error.message}`);
};
```
## 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

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

174
## router.pushUrl<sup>9+</sup>
W
wusongqing 已提交
175

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

178
Navigates to a specified page in the application.
W
wusongqing 已提交
179 180 181 182

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

**Parameters**
W
wusongqing 已提交
183

184 185 186 187 188 189 190 191 192 193 194 195
| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 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|
| --------- | ------- |
E
ester.zhou 已提交
196 197 198
| 100001    | If UI execution context not found. |
| 100002    | If the uri is not exist. |
| 100003    | If the pages are pushed too much. |
199 200 201 202 203 204 205 206 207 208 209

**Example**

```js
try {
  router.pushUrl({
    url: 'pages/routerpage2',
    params: {
      data1: 'message',
      data2: {
        data3: [123, 456, 789]
E
ester.zhou 已提交
210 211
      }
    }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  }, router.RouterMode.Standard, (err) => {
    if (err) {
      console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    console.info('pushUrl success');
  });
} catch (error) {
  console.error(`pushUrl args error code is ${error.code}, message is ${error.message}`);
};
```

## 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 已提交
234 235 236
| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | Yes  | Description of the new page.|
W
wusongqing 已提交
237

238 239 240 241 242 243 244 245 246 247 248 249
**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 已提交
250 251
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
252

W
wusongqing 已提交
253
**Example**
W
wusongqing 已提交
254

W
wusongqing 已提交
255
```js
256 257 258 259
try {
  router.replaceUrl({
    url: 'pages/detail',
    params: {
E
ester.zhou 已提交
260 261
      data1: 'message'
    }
262 263 264 265 266 267 268 269 270 271
  })
    .then(() => {
      // success
    })
    .catch(err => {
      console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
    })
} catch (error) {
  console.error(`replaceUrl args error code is ${error.code}, message is ${error.message}`);
};
W
wusongqing 已提交
272
```
W
wusongqing 已提交
273

274
## router.replaceUrl<sup>9+</sup>
W
wusongqing 已提交
275

276
replaceUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
277 278 279

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

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
**System capability**: SystemCapability.ArkUI.ArkUI.Lite

**Parameters**

| Name | Type                           | Mandatory| Description              |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | 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 已提交
295 296
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
297 298 299 300 301 302 303 304

**Example**

```js
try {
  router.replaceUrl({
    url: 'pages/detail',
    params: {
E
ester.zhou 已提交
305 306
      data1: 'message'
    }
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  }, (err) => {
    if (err) {
      console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    console.info('replaceUrl success');
  });
} catch (error) {
  console.error(`replaceUrl args error code is ${error.code}, message is ${error.message}`);
};
```

## 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 已提交
326 327

**Parameters**
328

W
wusongqing 已提交
329 330 331 332
| Name    | Type                             | Mandatory  | Description        |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | Yes   | Description of the new page. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|
W
wusongqing 已提交
333

334 335 336 337 338 339 340 341 342 343 344 345 346

**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 已提交
347 348
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
349

W
wusongqing 已提交
350 351
**Example**

W
wusongqing 已提交
352
```js
353 354 355 356
try {
  router.replaceUrl({
    url: 'pages/detail',
    params: {
E
ester.zhou 已提交
357 358
      data1: 'message'
    }
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  }, router.RouterMode.Standard)
    .then(() => {
      // success
    })
    .catch(err => {
      console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
    })
} catch (error) {
  console.error(`replaceUrl args error code is ${error.code}, message is ${error.message}`);
};
```

## 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.|
| 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 已提交
393 394
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
395 396 397 398 399 400 401 402

**Example**

```js
try {
  router.replaceUrl({
    url: 'pages/detail',
    params: {
E
ester.zhou 已提交
403 404
      data1: 'message'
    }
405 406 407 408 409 410 411 412 413 414
  }, router.RouterMode.Standard, (err) => {
    if (err) {
      console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    console.info('replaceUrl success');
  });
} catch (error) {
  console.error(`replaceUrl args error code is ${error.code}, message is ${error.message}`);
};
W
wusongqing 已提交
415
```
W
wusongqing 已提交
416 417 418 419 420 421 422 423 424 425

## router.back

back(options?: RouterOptions ): void

Returns to the previous page or a specified page.

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

**Parameters**
426

W
wusongqing 已提交
427 428
| Name | Type                           | Mandatory| Description                                                        |
| ------- | ------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
429
| 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 previous page is returned, and the page in the page stack is not reclaimed. It will be reclaimed after being popped up.|
W
wusongqing 已提交
430 431 432

**Example**

W
wusongqing 已提交
433 434 435
```js
router.back({url:'pages/detail'});    
```
W
wusongqing 已提交
436 437 438 439 440 441 442 443 444 445

## 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 已提交
446

W
wusongqing 已提交
447
```js
W
wusongqing 已提交
448
router.clear();    
W
wusongqing 已提交
449
```
W
wusongqing 已提交
450 451 452 453 454 455 456

## router.getLength

getLength(): string

Obtains the number of pages in the current stack.

W
wusongqing 已提交
457 458
**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
459
**Return value**
460

W
wusongqing 已提交
461 462
| Type    | Description                |
| ------ | ------------------ |
W
wusongqing 已提交
463
| string | Number of pages in the stack. The maximum value is **32**.|
W
wusongqing 已提交
464 465

**Example**
466

W
wusongqing 已提交
467
```js
E
ester.zhou 已提交
468
let size = router.getLength();        
W
wusongqing 已提交
469
console.log('pages stack size = ' + size);    
W
wusongqing 已提交
470
```
W
wusongqing 已提交
471 472 473 474 475 476 477 478 479 480 481

## router.getState

getState(): RouterState

Obtains state information about the current page.

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

**Return value**

W
wusongqing 已提交
482 483
| Type                         | Description     |
| --------------------------- | ------- |
W
wusongqing 已提交
484
| [RouterState](#routerstate) | Page routing state.|
G
Gloria 已提交
485

W
wusongqing 已提交
486 487 488
**Example**

```js
E
ester.zhou 已提交
489
let page = router.getState();
W
wusongqing 已提交
490 491 492 493 494
console.log('current index = ' + page.index);
console.log('current name = ' + page.name);
console.log('current path = ' + page.path);
```

W
wusongqing 已提交
495
## RouterState
W
wusongqing 已提交
496

W
wusongqing 已提交
497 498 499 500
Describes the page routing state.

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

E
ester.zhou 已提交
501 502 503 504 505
| 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 已提交
506

507
## router.enableBackPageAlert<sup>9+</sup>
W
wusongqing 已提交
508

509
enableBackPageAlert(options: EnableAlertOptions): void
W
wusongqing 已提交
510 511 512 513 514 515

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

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

**Parameters**
516

W
wusongqing 已提交
517 518 519
| Name    | Type                                      | Mandatory  | Description       |
| ------- | ---------------------------------------- | ---- | --------- |
| options | [EnableAlertOptions](#enablealertoptions) | Yes   | Description of the dialog box.|
W
wusongqing 已提交
520

521 522 523 524 525 526
**Error codes**

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

| ID  | Error Message|
| --------- | ------- |
E
ester.zhou 已提交
527
| 100001    | If UI execution context not found. |
528

W
wusongqing 已提交
529 530
**Example**

531 532 533
  ```js    
try {
  router.enableBackPageAlert({            
W
wusongqing 已提交
534
    message: 'Message Info'        
535 536 537 538
  });
} catch(error) {
  console.error(`enableBackPageAlert failed, code is ${error.code}, message is ${error.message}`);
}
W
wusongqing 已提交
539 540 541 542 543
  ```
## EnableAlertOptions

Describes the confirm dialog box.

W
wusongqing 已提交
544
**System capability**: SystemCapability.ArkUI.ArkUI.Full
W
wusongqing 已提交
545

W
wusongqing 已提交
546 547 548
| Name     | Type    | Mandatory  | Description      |
| ------- | ------ | ---- | -------- |
| message | string | Yes   | Content displayed in the confirm dialog box.|
W
wusongqing 已提交
549 550 551 552 553 554 555 556 557 558

## router.disableAlertBeforeBackPage

disableAlertBeforeBackPage(): void

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

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

**Example**
559

W
wusongqing 已提交
560
```js
W
wusongqing 已提交
561
router.disableAlertBeforeBackPage();    
W
wusongqing 已提交
562
```
W
wusongqing 已提交
563 564 565 566 567 568 569 570 571 572 573

##  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**

W
wusongqing 已提交
574 575
| Type    | Description               |
| ------ | ----------------- |
W
wusongqing 已提交
576 577 578 579
| Object | Parameters passed from the page that initiates redirection to the current page.|

**Example**

W
wusongqing 已提交
580 581 582 583 584 585 586 587
```
router.getParams();
```

## RouterOptions

Describes the page routing options.

W
wusongqing 已提交
588
**System capability**: SystemCapability.ArkUI.ArkUI.Lite
W
wusongqing 已提交
589

590 591 592
| 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 已提交
593
| 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 已提交
594 595 596 597 598 599 600 601 602 603 604


  > **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 已提交
605 606 607
| Name    | Description                                                        |
| -------- | ------------------------------------------------------------ |
| Standard | Standard 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.|
W
wusongqing 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620
| 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 as a new page 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 standard mode.|

## Examples

### JavaScript-based Web-like Development Paradigm

```js
// Current page
export default {
  pushPage() {
    router.push({
      url: 'pages/detail/detail',
      params: {
E
ester.zhou 已提交
621 622
        data1: 'message'
      }
W
wusongqing 已提交
623
    });
W
wusongqing 已提交
624
  }
W
wusongqing 已提交
625 626 627 628 629 630
}
```
```js
// detail page
export default {
  onInit() {
W
wusongqing 已提交
631
    console.info('showData1:' + router.getParams()[data1]);
W
wusongqing 已提交
632
  }
W
wusongqing 已提交
633 634
}
```
W
wusongqing 已提交
635

W
wusongqing 已提交
636
### TypeScript-based Declarative Development Paradigm
W
wusongqing 已提交
637

W
wusongqing 已提交
638 639 640 641 642 643 644
```ts
// Navigate to the target page through router.push with the params parameter carried.
import router from '@ohos.router'

@Entry
@Component
struct Index {
E
ester.zhou 已提交
645
  async routePage() {
W
wusongqing 已提交
646 647 648 649 650 651
    let options = {
      url: 'pages/second',
      params: {
        text: 'This is the value on the first page.',
        data: {
          array: [12, 45, 78]
E
ester.zhou 已提交
652
        }
W
wusongqing 已提交
653 654
      }
    }
W
wusongqing 已提交
655
    try {
E
ester.zhou 已提交
656
      await router.pushUrl(options)
W
wusongqing 已提交
657 658
    } catch (err) {
      console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
W
wusongqing 已提交
659 660 661
    }
  }

W
wusongqing 已提交
662 663
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
E
ester.zhou 已提交
664 665 666
      Text('This is the first page.')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
W
wusongqing 已提交
667 668 669 670 671
      Button() {
        Text('next page')
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
      }.type(ButtonType.Capsule)
E
ester.zhou 已提交
672 673 674 675
      .margin({ top: 20 })
      .backgroundColor('#ccc')
      .onClick(() => {
        this.routePage()
W
wusongqing 已提交
676
      })
W
wusongqing 已提交
677
    }
W
wusongqing 已提交
678 679
    .width('100%')
    .height('100%')
W
wusongqing 已提交
680
  }
W
wusongqing 已提交
681 682
}
```
W
wusongqing 已提交
683

W
wusongqing 已提交
684 685 686
```ts
// Receive the transferred parameters on the second page.
import router from '@ohos.router'
W
wusongqing 已提交
687

W
wusongqing 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
@Entry
@Component
struct Second {
  private content: string = "This is the second page."
  @State text: string = router.getParams()['text']
  @State data: any = router.getParams()['data']
  @State secondData : string = ''
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text(`${this.content}`)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
      Text(this.text)
        .fontSize(30)
        .onClick(()=>{
          this.secondData = (this.data.array[1]).toString()
        })
      .margin({top:20})
E
ester.zhou 已提交
707
      Text(`This is the data passed from the first page: ${this.secondData}`)
W
wusongqing 已提交
708 709 710 711 712 713 714 715 716
        .fontSize(20)
        .margin({top:20})
        .backgroundColor('red')      
    }
    .width('100%')
    .height('100%')
  }
}
```
717 718 719 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.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**

```js
router.push({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
744 745
    }
  }
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
});
```
## router.push<sup>(deprecated)</sup>

push(options: RouterOptions, mode: RouterMode): 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. |
| mode    | [RouterMode](#routermode9)      | Yes   | Routing mode.|


**Example**

```js
router.push({
  url: 'pages/routerpage2/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
E
ester.zhou 已提交
775 776
    }
  }
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
},router.RouterMode.Standard);
```

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

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

**Parameters**

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

**Example**

```js
router.replace({
  url: 'pages/detail',
  params: {
E
ester.zhou 已提交
802 803
    data1: 'message'
  }
804 805 806
});
```

E
ester.zhou 已提交
807
## router.replace<sup>(deprecated)</sup>
808 809 810 811 812 813 814

replace(options: RouterOptions, mode: RouterMode): 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 已提交
815
**System capability**: SystemCapability.ArkUI.ArkUI.Lite
816 817 818 819 820 821 822 823 824 825 826 827 828 829

**Parameters**

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

**Example**

```js
router.replace({
  url: 'pages/detail/detail',
  params: {
E
ester.zhou 已提交
830 831
    data1: 'message'
  }
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
}, router.RouterMode.Standard);
```

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

enableAlertBeforeBackPage(options: EnableAlertOptions): void

Enables 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 [enableBackPageAlert<sup>9+</sup>](#routerenablebackpagealert9) instead.

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

**Parameters**

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

**Example**

  ```js        
  router.enableAlertBeforeBackPage({            
    message: 'Message Info'        
  });    
  ```