js-apis-router.md 22.3 KB
Newer Older
1
# @ohos.router (页面路由)
K
kukixi 已提交
2

T
explain  
tianyu 已提交
3 4
本模块提供通过不同的url访问不同的页面,包括跳转到应用内的指定页面、用应用内的某个页面替换当前页面、返回上一页面或指定的页面等。

5
> **说明**
K
kukixi 已提交
6 7
>
> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
H
HelloCrease 已提交
8
>
K
kukixi 已提交
9
> - 页面路由需要在页面渲染完成之后才能调用,在onInit和onReady生命周期中页面还处于渲染阶段,禁止调用页面路由方法。
K
kukixi 已提交
10 11 12 13 14 15 16

## 导入模块

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

Z
zhaoxinyu 已提交
17
## router.pushUrl<sup>9+</sup>
K
kukixi 已提交
18

Z
zhaoxinyu 已提交
19
pushUrl(options: RouterOptions): Promise&lt;void&gt;
K
kukixi 已提交
20 21 22 23 24 25

跳转到应用内的指定页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
Z
zhaoxinyu 已提交
26

H
HelloCrease 已提交
27 28 29
| 参数名     | 类型                              | 必填   | 说明        |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。 |
K
kukixi 已提交
30

Z
zhaoxinyu 已提交
31 32 33 34
**返回值:**

| 类型                | 说明        |
| ------------------- | --------- |
35
| Promise&lt;void&gt; | 异常返回结果。 |
Z
zhaoxinyu 已提交
36 37 38 39 40

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

41
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
42
| --------- | ------- |
Y
yamila 已提交
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. |
K
kukixi 已提交
46 47

**示例:**
Z
zhaoxinyu 已提交
48

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

Z
zhaoxinyu 已提交
67 68 69
## router.pushUrl<sup>9+</sup>

pushUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void
70 71 72 73 74 75

跳转到应用内的指定页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
Z
zhaoxinyu 已提交
76 77 78 79

| 参数名     | 类型                              | 必填   | 说明        |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。 |
80
| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
Z
zhaoxinyu 已提交
81 82 83 84 85

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

86
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
87
| --------- | ------- |
Y
yamila 已提交
88 89 90
| 100001    | If UI execution context not found. |
| 100002    | If the uri is not exist. |
| 100003    | If the pages are pushed too much. |
Z
zhaoxinyu 已提交
91 92 93 94

**示例:**

```js
Y
yamila 已提交
95 96 97 98 99 100
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
101
    }
Y
yamila 已提交
102 103 104 105 106 107 108 109
  }
}, (err) => {
  if (err) {
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('pushUrl success');
});
Z
zhaoxinyu 已提交
110 111 112 113 114 115 116 117 118 119 120
```
## router.pushUrl<sup>9+</sup>

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

跳转到应用内的指定页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**

H
HelloCrease 已提交
121 122 123 124
| 参数名     | 类型                              | 必填   | 说明         |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。  |
| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
125

Z
zhaoxinyu 已提交
126 127 128 129
**返回值:**

| 类型                | 说明        |
| ------------------- | --------- |
Y
yamila 已提交
130
| Promise&lt;void&gt; | 异常返回结果。 |
Z
zhaoxinyu 已提交
131 132 133 134 135

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

136
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
137
| --------- | ------- |
Y
yamila 已提交
138 139 140
| 100001    | If UI execution context not found. |
| 100002    | If the uri is not exist. |
| 100003    | If the pages are pushed too much. |
141 142

**示例:**
Z
zhaoxinyu 已提交
143

144
```js
Y
yamila 已提交
145 146 147 148 149 150
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
151
    }
Y
yamila 已提交
152 153 154 155 156 157 158 159
  }
}, router.RouterMode.Standard)
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  })
160
```
K
kukixi 已提交
161

Z
zhaoxinyu 已提交
162
## router.pushUrl<sup>9+</sup>
K
kukixi 已提交
163

Z
zhaoxinyu 已提交
164
pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void
K
kukixi 已提交
165

Z
zhaoxinyu 已提交
166
跳转到应用内的指定页面。
K
kukixi 已提交
167 168 169 170

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
T
tianyu 已提交
171

Z
zhaoxinyu 已提交
172 173 174 175
| 参数名     | 类型                              | 必填   | 说明         |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。  |
| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
176
| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。    |
Z
zhaoxinyu 已提交
177 178 179 180 181

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

182
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
183
| --------- | ------- |
Y
yamila 已提交
184 185 186
| 100001    | If UI execution context not found. |
| 100002    | If the uri is not exist. |
| 100003    | If the pages are pushed too much. |
Z
zhaoxinyu 已提交
187 188 189 190

**示例:**

```js
Y
yamila 已提交
191 192 193 194 195 196
router.pushUrl({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
Z
zhaoxinyu 已提交
197
    }
Y
yamila 已提交
198 199 200 201 202 203 204 205
  }
}, router.RouterMode.Standard, (err) => {
  if (err) {
    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('pushUrl success');
});
Z
zhaoxinyu 已提交
206 207 208 209 210 211 212 213 214 215 216 217
```

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

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

用应用内的某个页面替换当前页面,并销毁被替换的页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Lite

**参数:**

218 219 220
| 参数名  | 类型                            | 必填 | 说明               |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | 是   | 替换页面描述信息。 |
K
kukixi 已提交
221

Z
zhaoxinyu 已提交
222 223 224 225
**返回值:**

| 类型                | 说明        |
| ------------------- | --------- |
226
| Promise&lt;void&gt; | 异常返回结果。 |
Z
zhaoxinyu 已提交
227 228 229 230 231

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

232
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
233
| --------- | ------- |
Y
yamila 已提交
234 235
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
Z
zhaoxinyu 已提交
236

K
kukixi 已提交
237
**示例:**
G
gmy 已提交
238

239
```js
Y
yamila 已提交
240 241 242 243 244 245 246 247 248 249 250
router.replaceUrl({
  url: 'pages/detail',
  params: {
    data1: 'message'
  }
})
  .then(() => {
    // success
  })
  .catch(err => {
    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
Z
zhaoxinyu 已提交
251
  })
252
```
K
kukixi 已提交
253

Z
zhaoxinyu 已提交
254
## router.replaceUrl<sup>9+</sup>
255

Z
zhaoxinyu 已提交
256
replaceUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void
257 258 259

用应用内的某个页面替换当前页面,并销毁被替换的页面。

Z
zhaoxinyu 已提交
260
**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
261 262

**参数:**
Z
zhaoxinyu 已提交
263 264 265 266

| 参数名  | 类型                            | 必填 | 说明               |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | 是   | 替换页面描述信息。 |
267
| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。    |
Z
zhaoxinyu 已提交
268 269 270 271 272

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

273
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
274
| --------- | ------- |
Y
yamila 已提交
275 276
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
Z
zhaoxinyu 已提交
277 278 279 280

**示例:**

```js
Y
yamila 已提交
281 282 283 284 285 286 287 288 289 290 291 292
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');
});
Z
zhaoxinyu 已提交
293 294 295 296 297 298 299 300 301 302 303 304
```

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

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

用应用内的某个页面替换当前页面,并销毁被替换的页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Lite

**参数:**

H
HelloCrease 已提交
305 306 307 308
| 参数名     | 类型                              | 必填   | 说明         |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 是    | 替换页面描述信息。  |
| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
309

Z
zhaoxinyu 已提交
310 311 312 313 314

**返回值:**

| 类型                | 说明        |
| ------------------- | --------- |
315
| Promise&lt;void&gt; | 异常返回结果。 |
Z
zhaoxinyu 已提交
316 317 318 319 320

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

321
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
322
| --------- | ------- |
Y
yamila 已提交
323 324
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
Z
zhaoxinyu 已提交
325

326 327
**示例:**

328
```js
Y
yamila 已提交
329 330 331 332 333 334 335 336 337 338 339 340
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}`);
  })
Z
zhaoxinyu 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
```

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

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

用应用内的某个页面替换当前页面,并销毁被替换的页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Lite

**参数:**

| 参数名     | 类型                              | 必填   | 说明         |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 是    | 替换页面描述信息。  |
| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
357
| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。  |
Z
zhaoxinyu 已提交
358 359 360 361 362

**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

363
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
364
| --------- | ------- |
Y
yamila 已提交
365 366
| 100001    | If UI execution context not found, only throw in standard system. |
| 200002    | If the uri is not exist. |
Z
zhaoxinyu 已提交
367 368 369 370

**示例:**

```js
Y
yamila 已提交
371 372 373 374 375 376 377 378 379 380 381 382
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');
});
383
```
K
kukixi 已提交
384 385 386 387 388 389 390 391 392 393

## router.back

back(options?: RouterOptions ): void

返回上一页面或指定的页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
Z
zhaoxinyu 已提交
394

395 396
| 参数名  | 类型                            | 必填 | 说明                                                         |
| ------- | ------------------------------- | ---- | ------------------------------------------------------------ |
T
tianyu 已提交
397
| options | [RouterOptions](#routeroptions) | 否   | 返回页面描述信息,其中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不响应该情况。如果url未设置,则返回上一页,页面栈里面的page不会回收,出栈后会被回收。 |
K
kukixi 已提交
398 399 400

**示例:**

401
```js
T
tianyu 已提交
402
router.back({url:'pages/detail'});    
403
```
K
kukixi 已提交
404 405 406 407 408 409 410 411 412 413

## router.clear

clear(): void

清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**示例:**
414

415
```js
416
router.clear();    
417
```
K
kukixi 已提交
418 419 420 421 422 423 424

## router.getLength

getLength(): string

获取当前在页面栈内的页面数量。

L
luoying_ace_admin 已提交
425 426
**系统能力:** SystemCapability.ArkUI.ArkUI.Full

K
kukixi 已提交
427
**返回值:**
Z
zhaoxinyu 已提交
428

H
HelloCrease 已提交
429 430
| 类型     | 说明                 |
| ------ | ------------------ |
G
gmy 已提交
431
| string | 页面数量,页面栈支持最大数值是32。 |
K
kukixi 已提交
432 433

**示例:**
Z
zhaoxinyu 已提交
434

435
```js
436
let size = router.getLength();        
437
console.log('pages stack size = ' + size);    
438
```
K
kukixi 已提交
439 440 441 442 443 444 445 446 447 448 449

## router.getState

getState(): RouterState

获取当前页面的状态信息。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**返回值:**

H
HelloCrease 已提交
450 451
| 类型                          | 说明      |
| --------------------------- | ------- |
452
| [RouterState](#routerstate) | 页面状态信息。 |
453

454 455 456
**示例:** 

```js
457
let page = router.getState();
458 459 460 461 462
console.log('current index = ' + page.index);
console.log('current name = ' + page.name);
console.log('current path = ' + page.path);
```

K
kukixi 已提交
463 464
## RouterState

465
页面状态信息。
K
kukixi 已提交
466

467
**系统能力:** SystemCapability.ArkUI.ArkUI.Full。
K
kukixi 已提交
468

Y
yamila 已提交
469 470 471 472 473
| 名称  | 类型   | 必填 | 说明                                                         |
| ----- | ------ | ---- | ------------------------------------------------------------ |
| index | number | 是   | 表示当前页面在页面栈中的索引。从栈底到栈顶,index从1开始递增。 |
| name  | string | 否   | 表示当前页面的名称,即对应文件名。                           |
| path  | string | 是   | 表示当前页面的路径。                                         |
K
kukixi 已提交
474

Z
zhaoxinyu 已提交
475
## router.enableBackPageAlert<sup>9+</sup>
K
kukixi 已提交
476

Z
zhaoxinyu 已提交
477
enableBackPageAlert(options: EnableAlertOptions): void
K
kukixi 已提交
478 479 480 481 482 483

开启页面返回询问对话框。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
Z
zhaoxinyu 已提交
484

H
HelloCrease 已提交
485 486 487
| 参数名     | 类型                                       | 必填   | 说明        |
| ------- | ---------------------------------------- | ---- | --------- |
| options | [EnableAlertOptions](#enablealertoptions) | 是    | 文本弹窗信息描述。 |
K
kukixi 已提交
488

Z
zhaoxinyu 已提交
489 490 491 492
**错误码:**

以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。

493
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
494
| --------- | ------- |
Y
yamila 已提交
495
| 100001    | If UI execution context not found. |
Z
zhaoxinyu 已提交
496

K
kukixi 已提交
497 498
**示例:**

Z
zhaoxinyu 已提交
499 500 501
  ```js    
try {
  router.enableBackPageAlert({            
502
    message: 'Message Info'        
Z
zhaoxinyu 已提交
503 504 505 506
  });
} catch(error) {
  console.error(`enableBackPageAlert failed, code is ${error.code}, message is ${error.message}`);
}
K
kukixi 已提交
507 508 509
  ```
## EnableAlertOptions

K
kukixi 已提交
510 511
页面返回询问对话框选项。

L
luoying_ace_admin 已提交
512
**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full。
K
kukixi 已提交
513

H
HelloCrease 已提交
514 515 516
| 名称      | 类型     | 必填   | 说明       |
| ------- | ------ | ---- | -------- |
| message | string | 是    | 询问对话框内容。 |
K
kukixi 已提交
517 518 519 520 521 522 523 524 525 526

## router.disableAlertBeforeBackPage

disableAlertBeforeBackPage(): void

禁用页面返回询问对话框。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**示例:**
Z
zhaoxinyu 已提交
527

528
```js
529
router.disableAlertBeforeBackPage();    
530
```
K
kukixi 已提交
531 532 533 534 535 536 537 538 539 540 541

##  router.getParams

getParams(): Object

获取发起跳转的页面往当前页传入的参数。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**返回值:**

Y
yamila 已提交
542 543 544
| 类型   | 说明                               |
| ------ | ---------------------------------- |
| object | 发起跳转的页面往当前页传入的参数。 |
K
kukixi 已提交
545

K
kukixi 已提交
546 547
**示例:**

548 549 550
```
router.getParams();
```
K
kukixi 已提交
551 552 553 554 555

## RouterOptions

路由跳转选项。

556
**系统能力:** SystemCapability.ArkUI.ArkUI.Lite。
K
kukixi 已提交
557

T
tianyu 已提交
558 559 560
| 名称   | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| url    | string | 是   | 表示目标页面的url,可以用以下两种格式:<br/>-&nbsp;页面绝对路径,由配置文件中pages列表提供,例如:<br/>&nbsp;&nbsp;-&nbsp;pages/index/index<br/>&nbsp;&nbsp;-&nbsp;pages/detail/detail<br/>-&nbsp;特殊值,如果url的值是"/",则跳转到首页。 |
Y
yamila 已提交
561
| params | object | 否   | 表示路由跳转时要同时传递到目标页面的数据。跳转到目标页面后,使用router.getParams()获取传递的参数,此外,在类web范式中,参数也可以在页面中直接使用,如this.keyValue(keyValue为跳转时params参数中的key值),如果目标页面中已有该字段,则其值会被传入的字段值覆盖。 |
K
kukixi 已提交
562 563


564
  > **说明:**
K
kukixi 已提交
565 566
  > 页面路由栈支持的最大Page数量为32。

H
huzeqi 已提交
567 568 569 570
## RouterMode<sup>9+</sup>

路由跳转模式。

571
**系统能力:** SystemCapability.ArkUI.ArkUI.Full。
572

Y
yamila 已提交
573 574 575
| 名称     | 说明                                                         |
| -------- | ------------------------------------------------------------ |
| Standard | 标准模式。 <br/>目标页面会被添加到页面路由栈顶,无论栈中是否存在相同url的页面。 |
576 577 578 579 580 581 582 583 584 585 586 587 588
| Single   | 单实例模式。<br/>如果目标页面的url在页面栈中已经存在同url页面,离栈顶最近的页面会被移动到栈顶,移动后的页面为新建页。<br/>如目标页面的url在页面栈中不存在同url页面,按标准模式跳转。 |

## 完整示例

### 基于JS扩展的类Web开发范式

```js
// 在当前页面中
export default {
  pushPage() {
    router.push({
      url: 'pages/detail/detail',
      params: {
589 590
        data1: 'message'
      }
591 592 593 594 595 596 597 598
    });
  }
}
```
```js
// 在detail页面中
export default {
  onInit() {
T
tianyu 已提交
599
    console.info('showData1:' + router.getParams()[data1]);
600 601 602 603 604
  }
}
```

### 基于TS扩展的声明式开发范式
H
huzeqi 已提交
605

606
```ts
Y
yamila 已提交
607
// 通过router.pushUrl跳转至目标页携带params参数
608 609 610 611 612 613 614 615 616 617 618 619
import router from '@ohos.router'

@Entry
@Component
struct Index {
  async  routePage() {
    let options = {
      url: 'pages/second',
      params: {
        text: '这是第一页的值',
        data: {
          array: [12, 45, 78]
620
        }
621 622 623
      }
    }
    try {
Y
yamila 已提交
624
      await router.pushUrl(options)
625 626 627 628 629 630 631
    } catch (err) {
      console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
    }
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Y
yamila 已提交
632 633 634
      Text('这是第一页')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
635 636 637 638 639
      Button() {
        Text('next page')
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
      }.type(ButtonType.Capsule)
Y
yamila 已提交
640 641 642 643
      .margin({ top: 20 })
      .backgroundColor('#ccc')
      .onClick(() => {
        this.routePage()
644 645 646 647 648 649 650 651 652
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

```ts
653
// 在second页面中接收传递过来的参数
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
import router from '@ohos.router'

@Entry
@Component
struct Second {
  private content: string = "这是第二页"
  @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})
Y
yamila 已提交
675
      Text(`第一页传来的数值:${this.secondData}`)
676 677 678 679 680 681 682 683
        .fontSize(20)
        .margin({top:20})
        .backgroundColor('red')      
    }
    .width('100%')
    .height('100%')
  }
}
Z
zhaoxinyu 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
```

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

push(options: RouterOptions): void

跳转到应用内的指定页面。

从API version9开始不再维护,建议使用[pushUrl<sup>9+</sup>](#routerpushurl9)

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**

| 参数名     | 类型                              | 必填   | 说明        |
| ------- | ------------------------------- | ---- | --------- |
| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。 |


**示例:**

```js
router.push({
  url: 'pages/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
712 713
    }
  }
Z
zhaoxinyu 已提交
714 715 716 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
});
```
## router.push<sup>(deprecated)</sup>

push(options: RouterOptions, mode: RouterMode): void

跳转到应用内的指定页面。

从API version9开始不再维护,建议使用[pushUrl<sup>9+</sup>](#routerpushurl9)

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**

| 参数名     | 类型                              | 必填   | 说明         |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。  |
| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |


**示例:**

```js
router.push({
  url: 'pages/routerpage2/routerpage2',
  params: {
    data1: 'message',
    data2: {
      data3: [123, 456, 789]
743 744
    }
  }
Z
zhaoxinyu 已提交
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
},router.RouterMode.Standard);
```

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

replace(options: RouterOptions): void

用应用内的某个页面替换当前页面,并销毁被替换的页面。

从API version9开始不再维护,建议使用[replaceUrl<sup>9+</sup>](#routerreplaceurl9)

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**

| 参数名  | 类型                            | 必填 | 说明               |
| ------- | ------------------------------- | ---- | ------------------ |
| options | [RouterOptions](#routeroptions) | 是   | 替换页面描述信息。 |

**示例:**

```js
router.replace({
  url: 'pages/detail',
  params: {
770 771
    data1: 'message'
  }
Z
zhaoxinyu 已提交
772 773 774
});
```

775
## router.replace<sup>(deprecated)</sup>
Z
zhaoxinyu 已提交
776 777 778 779 780 781 782

replace(options: RouterOptions, mode: RouterMode): void

用应用内的某个页面替换当前页面,并销毁被替换的页面。

从API version9开始不再维护,建议使用[replaceUrl<sup>9+</sup>](#routerreplaceurl9)

Y
yamila 已提交
783
**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
Z
zhaoxinyu 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797

**参数:**

| 参数名     | 类型                              | 必填   | 说明         |
| ------- | ------------------------------- | ---- | ---------- |
| options | [RouterOptions](#routeroptions) | 是    | 替换页面描述信息。  |
| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |

**示例:**

```js
router.replace({
  url: 'pages/detail/detail',
  params: {
798 799
    data1: 'message'
  }
Z
zhaoxinyu 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
}, router.RouterMode.Standard);
```

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

enableAlertBeforeBackPage(options: EnableAlertOptions): void

开启页面返回询问对话框。

从API version9开始不再维护,建议使用[enableBackPageAlert<sup>9+</sup>](#routerenablebackpagealert9)

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**

| 参数名     | 类型                                       | 必填   | 说明        |
| ------- | ---------------------------------------- | ---- | --------- |
| options | [EnableAlertOptions](#enablealertoptions) | 是    | 文本弹窗信息描述。 |

**示例:**

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