• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.router (页面路由)(不推荐)
2
3本模块提供通过不同的url访问不同的页面,包括跳转到应用内的指定页面、同应用内的某个页面替换当前页面、返回上一页面或指定的页面等。
4
5推荐使用[Navigation组件](../../ui/arkts-navigation-navigation.md)作为应用路由框架。
6
7> **说明**
8>
9> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10>
11> - 页面路由需要在页面渲染完成之后才能调用,在onInit和onReady生命周期中页面还处于渲染阶段,禁止调用页面路由方法。
12>
13> - 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](./js-apis-arkui-UIContext.md#uicontext)说明。
14>
15> - 从API version 10开始,可以通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getRouter](./js-apis-arkui-UIContext.md#getrouter)方法获取当前UI上下文关联的[Router](./js-apis-arkui-UIContext.md#router)对象。
16>
17> - 如果使用传入callback形式的[pushUrl](#routerpushurl9-1)或[pushNamedRoute](#routerpushnamedroute10-1)接口,callback中通过[getLength](#routergetlength)等接口获取的栈信息为中间态的栈信息,可能与栈操作完全结束后,再通过[getLength](#routergetlength)等接口获取的栈信息不一致。
18
19## 导入模块
20
21```
22import { router } from '@kit.ArkUI';
23```
24
25## router.pushUrl<sup>9+</sup>
26
27pushUrl(options: RouterOptions): Promise&lt;void&gt;
28
29跳转到应用内的指定页面。
30
31**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
32
33**系统能力:** SystemCapability.ArkUI.ArkUI.Full
34
35**参数:**
36
37| 参数名     | 类型                              | 必填   | 说明        |
38| ------- | ------------------------------- | ---- | --------- |
39| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。 |
40
41**返回值:**
42
43| 类型                | 说明        |
44| ------------------- | --------- |
45| Promise&lt;void&gt; | 异常返回结果。 |
46
47**错误码:**
48
49以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
50> **说明**:
51>
52> 该接口返回的以下错误码均为string类型。
53
54| 错误码ID   | 错误信息 |
55| --------- | ------- |
56| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
57| 100001    | Internal error. |
58| 100002    | Uri error. The URI of the page to redirect is incorrect or does not exist. |
59| 100003    | Page stack error. Too many pages are pushed. |
60
61**示例:**
62
63```ts
64import { BusinessError } from '@kit.BasicServicesKit';
65
66class innerParams {
67  data3: number[]
68
69  constructor(tuple: number[]) {
70    this.data3 = tuple
71  }
72}
73
74class routerParams {
75  data1: string
76  data2: innerParams
77
78  constructor(str: string, tuple: number[]) {
79    this.data1 = str
80    this.data2 = new innerParams(tuple)
81  }
82}
83
84router.pushUrl({
85  url: 'pages/routerpage2',
86  params: new routerParams('message', [123, 456, 789])
87})
88  .then(() => {
89    console.error(`pushUrl finish`);
90  })
91  .catch((err: ESObject) => {
92    console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
93  })
94```
95
96## router.pushUrl<sup>9+</sup>
97
98pushUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void
99
100跳转到应用内的指定页面。
101
102**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
103
104**系统能力:** SystemCapability.ArkUI.ArkUI.Full
105
106**参数:**
107
108| 参数名     | 类型                              | 必填   | 说明        |
109| ------- | ------------------------------- | ---- | --------- |
110| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。 |
111| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
112
113**错误码:**
114
115以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
116> **说明**:
117>
118> 该接口返回的以下错误码均为string类型。
119
120| 错误码ID   | 错误信息 |
121| --------- | ------- |
122| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
123| 100001    | Internal error. |
124| 100002    | Uri error. The URI of the page to redirect is incorrect or does not exist. |
125| 100003    | Page stack error. Too many pages are pushed. |
126
127**示例:**
128
129```ts
130class innerParams {
131  data3: number[]
132
133  constructor(tuple: number[]) {
134    this.data3 = tuple
135  }
136}
137
138class routerParams {
139  data1: string
140  data2: innerParams
141
142  constructor(str: string, tuple: number[]) {
143    this.data1 = str
144    this.data2 = new innerParams(tuple)
145  }
146}
147
148router.pushUrl({
149  url: 'pages/routerpage2',
150  params: new routerParams('message', [123, 456, 789])
151}, (err) => {
152  if (err) {
153    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
154    return;
155  }
156  console.info('pushUrl success');
157})
158```
159## router.pushUrl<sup>9+</sup>
160
161pushUrl(options: RouterOptions, mode: RouterMode): Promise&lt;void&gt;
162
163跳转到应用内的指定页面。
164
165**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
166
167**系统能力:** SystemCapability.ArkUI.ArkUI.Full
168
169**参数:**
170
171| 参数名     | 类型                              | 必填   | 说明         |
172| ------- | ------------------------------- | ---- | ---------- |
173| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。  |
174| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
175
176**返回值:**
177
178| 类型                | 说明        |
179| ------------------- | --------- |
180| Promise&lt;void&gt; | 异常返回结果。 |
181
182**错误码:**
183
184以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
185> **说明**:
186>
187> 该接口返回的以下错误码均为string类型。
188
189| 错误码ID   | 错误信息 |
190| --------- | ------- |
191| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
192| 100001    | Internal error. |
193| 100002    | Uri error. The URI of the page to redirect is incorrect or does not exist. |
194| 100003    | Page stack error. Too many pages are pushed. |
195
196**示例:**
197
198```ts
199import { BusinessError } from '@kit.BasicServicesKit';
200
201class innerParams {
202  data3: number[]
203
204  constructor(tuple: number[]) {
205    this.data3 = tuple
206  }
207}
208
209class routerParams {
210  data1: string
211  data2: innerParams
212
213  constructor(str: string, tuple: number[]) {
214    this.data1 = str
215    this.data2 = new innerParams(tuple)
216  }
217}
218
219router.pushUrl({
220  url: 'pages/routerpage2',
221  params: new routerParams('message', [123, 456, 789])
222}, router.RouterMode.Standard)
223  .then(() => {
224    console.error(`pushUrl finish`);
225  })
226  .catch((err: ESObject) => {
227    console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
228  })
229```
230
231## router.pushUrl<sup>9+</sup>
232
233pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void
234
235跳转到应用内的指定页面。
236
237**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
238
239**系统能力:** SystemCapability.ArkUI.ArkUI.Full
240
241**参数:**
242
243| 参数名     | 类型                              | 必填   | 说明         |
244| ------- | ------------------------------- | ---- | ---------- |
245| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。  |
246| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
247| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
248
249**错误码:**
250
251以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
252> **说明**:
253>
254> 该接口返回的以下错误码均为string类型。
255
256| 错误码ID   | 错误信息 |
257| --------- | ------- |
258| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
259| 100001    | Internal error. |
260| 100002    | Uri error. The URI of the page to redirect is incorrect or does not exist. |
261| 100003    | Page stack error. Too many pages are pushed. |
262
263**示例:**
264
265```ts
266class innerParams {
267  data3: number[]
268
269  constructor(tuple: number[]) {
270    this.data3 = tuple
271  }
272}
273
274class routerParams {
275  data1: string
276  data2: innerParams
277
278  constructor(str: string, tuple: number[]) {
279    this.data1 = str
280    this.data2 = new innerParams(tuple)
281  }
282}
283
284router.pushUrl({
285  url: 'pages/routerpage2',
286  params: new routerParams('message', [123, 456, 789])
287}, router.RouterMode.Standard, (err) => {
288  if (err) {
289    console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
290    return;
291  }
292  console.info('pushUrl success');
293})
294```
295
296## router.replaceUrl<sup>9+</sup>
297
298replaceUrl(options: RouterOptions): Promise&lt;void&gt;
299
300用应用内的某个页面替换当前页面,并销毁被替换的页面。不支持设置页面转场动效,如需设置,推荐使用[Navigation组件](../../ui/arkts-navigation-navigation.md)。
301
302**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
303
304**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
305
306**参数:**
307
308| 参数名  | 类型                            | 必填 | 说明               |
309| ------- | ------------------------------- | ---- | ------------------ |
310| options | [RouterOptions](#routeroptions) | 是   | 替换页面描述信息。 |
311
312**返回值:**
313
314| 类型                | 说明        |
315| ------------------- | --------- |
316| Promise&lt;void&gt; | 异常返回结果。 |
317
318**错误码:**
319
320以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
321> **说明**:
322>
323> 该接口返回的以下错误码均为string类型。
324
325| 错误码ID   | 错误信息 |
326| --------- | ------- |
327| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
328| 100001    | The UI execution context is not found. This error code is thrown only in the standard system. |
329| 200002    | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. |
330
331**示例:**
332
333```ts
334import { BusinessError } from '@kit.BasicServicesKit';
335
336class routerParams {
337  data1: string
338
339  constructor(str: string) {
340    this.data1 = str
341  }
342}
343
344router.replaceUrl({
345  url: 'pages/detail',
346  params: new routerParams('message')
347})
348  .then(() => {
349    console.error(`replaceUrl finish`);
350  })
351  .catch((err: ESObject) => {
352    console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
353  })
354```
355
356## router.replaceUrl<sup>9+</sup>
357
358replaceUrl(options: RouterOptions, callback: AsyncCallback&lt;void&gt;): void
359
360用应用内的某个页面替换当前页面,并销毁被替换的页面。
361
362**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
363
364**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
365
366**参数:**
367
368| 参数名  | 类型                            | 必填 | 说明               |
369| ------- | ------------------------------- | ---- | ------------------ |
370| options | [RouterOptions](#routeroptions) | 是   | 替换页面描述信息。 |
371| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
372
373**错误码:**
374
375以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
376> **说明**:
377>
378> 该接口返回的以下错误码均为string类型。
379
380| 错误码ID   | 错误信息 |
381| --------- | ------- |
382| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
383| 100001    | The UI execution context is not found. This error code is thrown only in the standard system. |
384| 200002    | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. |
385
386**示例:**
387
388```ts
389class routerParams {
390  data1: string
391
392  constructor(str: string) {
393    this.data1 = str
394  }
395}
396
397router.replaceUrl({
398  url: 'pages/detail',
399  params: new routerParams('message')
400}, (err) => {
401  if (err) {
402    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
403    return;
404  }
405  console.info('replaceUrl success');
406})
407```
408
409## router.replaceUrl<sup>9+</sup>
410
411replaceUrl(options: RouterOptions, mode: RouterMode): Promise&lt;void&gt;
412
413用应用内的某个页面替换当前页面,并销毁被替换的页面。
414
415**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
416
417**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
418
419**参数:**
420
421| 参数名     | 类型                              | 必填   | 说明         |
422| ------- | ------------------------------- | ---- | ---------- |
423| options | [RouterOptions](#routeroptions) | 是    | 替换页面描述信息。  |
424| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
425
426
427**返回值:**
428
429| 类型                | 说明        |
430| ------------------- | --------- |
431| Promise&lt;void&gt; | 异常返回结果。 |
432
433**错误码:**
434
435以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
436> **说明**:
437>
438> 该接口返回的以下错误码均为string类型。
439
440| 错误码ID   | 错误信息 |
441| --------- | ------- |
442| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
443| 100001    | Failed to get the delegate. This error code is thrown only in the standard system. |
444| 200002    | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. |
445
446**示例:**
447
448```ts
449import { BusinessError } from '@kit.BasicServicesKit';
450
451class routerParams {
452  data1:string
453
454  constructor(str:string) {
455    this.data1 = str
456  }
457}
458
459router.replaceUrl({
460  url: 'pages/detail',
461  params: new routerParams('message')
462}, router.RouterMode.Standard)
463  .then(() => {
464    console.error(`replaceUrl finish`);
465  })
466  .catch((err: ESObject) => {
467    console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
468  })
469```
470
471## router.replaceUrl<sup>9+</sup>
472
473replaceUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void
474
475用应用内的某个页面替换当前页面,并销毁被替换的页面。
476
477**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
478
479**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
480
481**参数:**
482
483| 参数名     | 类型                              | 必填   | 说明         |
484| ------- | ------------------------------- | ---- | ---------- |
485| options | [RouterOptions](#routeroptions) | 是    | 替换页面描述信息。  |
486| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
487| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
488
489**错误码:**
490
491以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
492> **说明**:
493>
494> 该接口返回的以下错误码均为string类型。
495
496| 错误码ID   | 错误信息 |
497| --------- | ------- |
498| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
499| 100001    | The UI execution context is not found. This error code is thrown only in the standard system. |
500| 200002    | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. |
501
502**示例:**
503
504```ts
505class routerParams {
506  data1: string
507
508  constructor(str: string) {
509    this.data1 = str
510  }
511}
512
513router.replaceUrl({
514  url: 'pages/detail',
515  params: new routerParams('message')
516}, router.RouterMode.Standard, (err) => {
517  if (err) {
518    console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
519    return;
520  }
521  console.info('replaceUrl success');
522});
523```
524
525## router.pushNamedRoute<sup>10+</sup>
526
527pushNamedRoute(options: NamedRouterOptions): Promise&lt;void&gt;
528
529跳转到指定的命名路由页面。
530
531**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
532
533**系统能力:** SystemCapability.ArkUI.ArkUI.Full
534
535**参数:**
536
537| 参数名     | 类型                              | 必填   | 说明        |
538| ------- | ------------------------------- | ---- | --------- |
539| options | [NamedRouterOptions](#namedrouteroptions10) | 是    | 跳转页面描述信息。 |
540
541**返回值:**
542
543| 类型                | 说明        |
544| ------------------- | --------- |
545| Promise&lt;void&gt; | 异常返回结果。 |
546
547**错误码:**
548
549以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
550> **说明**:
551>
552> 该接口返回的以下错误码均为string类型。
553
554| 错误码ID   | 错误信息 |
555| --------- | ------- |
556| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
557| 100001    | Internal error. |
558| 100003    | Page stack error. Too many pages are pushed. |
559| 100004    | Named route error. The named route does not exist. |
560
561**示例:**
562
563```ts
564import { BusinessError } from '@kit.BasicServicesKit';
565
566class innerParams {
567  data3: number[]
568
569  constructor(tuple: number[]) {
570    this.data3 = tuple
571  }
572}
573
574class routerParams {
575  data1: string
576  data2: innerParams
577
578  constructor(str: string, tuple: number[]) {
579    this.data1 = str
580    this.data2 = new innerParams(tuple)
581  }
582}
583
584router.pushNamedRoute({
585  name: 'myPage',
586  params: new routerParams('message', [123, 456, 789])
587})
588  .then(() => {
589    console.error(`pushNamedRoute finish`);
590  })
591  .catch((err: ESObject) => {
592    console.error(`pushNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
593  })
594```
595
596详细示例请参考:[UI开发-页面路由](../../ui/arkts-routing.md#命名路由)
597
598## router.pushNamedRoute<sup>10+</sup>
599
600pushNamedRoute(options: NamedRouterOptions, callback: AsyncCallback&lt;void&gt;): void
601
602跳转到指定的命名路由页面。
603
604**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
605
606**系统能力:** SystemCapability.ArkUI.ArkUI.Full
607
608**参数:**
609
610| 参数名     | 类型                              | 必填   | 说明        |
611| ------- | ------------------------------- | ---- | --------- |
612| options | [NamedRouterOptions](#namedrouteroptions10) | 是    | 跳转页面描述信息。 |
613| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
614
615**错误码:**
616
617以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
618> **说明**:
619>
620> 该接口返回的以下错误码均为string类型。
621
622| 错误码ID   | 错误信息 |
623| --------- | ------- |
624| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
625| 100001    | Internal error. |
626| 100003    | Page stack error. Too many pages are pushed. |
627| 100004    | Named route error. The named route does not exist. |
628
629**示例:**
630
631```ts
632class innerParams {
633  data3: number[]
634
635  constructor(tuple: number[]) {
636    this.data3 = tuple
637  }
638}
639
640class routerParams {
641  data1: string
642  data2: innerParams
643
644  constructor(str: string, tuple: number[]) {
645    this.data1 = str
646    this.data2 = new innerParams(tuple)
647  }
648}
649
650router.pushNamedRoute({
651  name: 'myPage',
652  params: new routerParams('message', [123, 456, 789])
653}, (err) => {
654  if (err) {
655    console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`);
656    return;
657  }
658  console.info('pushNamedRoute success');
659})
660```
661## router.pushNamedRoute<sup>10+</sup>
662
663pushNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise&lt;void&gt;
664
665跳转到指定的命名路由页面。
666
667**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
668
669**系统能力:** SystemCapability.ArkUI.ArkUI.Full
670
671**参数:**
672
673| 参数名     | 类型                              | 必填   | 说明         |
674| ------- | ------------------------------- | ---- | ---------- |
675| options | [NamedRouterOptions](#namedrouteroptions10) | 是    | 跳转页面描述信息。  |
676| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
677
678**返回值:**
679
680| 类型                | 说明        |
681| ------------------- | --------- |
682| Promise&lt;void&gt; | 异常返回结果。 |
683
684**错误码:**
685
686以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
687> **说明**:
688>
689> 该接口返回的以下错误码均为string类型。
690
691| 错误码ID   | 错误信息 |
692| --------- | ------- |
693| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
694| 100001    | Internal error. |
695| 100003    | Page stack error. Too many pages are pushed. |
696| 100004    | Named route error. The named route does not exist. |
697
698**示例:**
699
700```ts
701import { BusinessError } from '@kit.BasicServicesKit';
702
703class innerParams {
704  data3: number[]
705
706  constructor(tuple: number[]) {
707    this.data3 = tuple
708  }
709}
710
711class routerParams {
712  data1: string
713  data2: innerParams
714
715  constructor(str: string, tuple: number[]) {
716    this.data1 = str
717    this.data2 = new innerParams(tuple)
718  }
719}
720
721router.pushNamedRoute({
722  name: 'myPage',
723  params: new routerParams('message', [123, 456, 789])
724}, router.RouterMode.Standard)
725  .then(() => {
726    console.error(`pushNamedRoute finish`);
727  })
728  .catch((err: ESObject) => {
729    console.error(`pushNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
730  })
731```
732
733## router.pushNamedRoute<sup>10+</sup>
734
735pushNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void
736
737跳转到指定的命名路由页面。
738
739**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
740
741**系统能力:** SystemCapability.ArkUI.ArkUI.Full
742
743**参数:**
744
745| 参数名     | 类型                              | 必填   | 说明         |
746| ------- | ------------------------------- | ---- | ---------- |
747| options | [NamedRouterOptions](#namedrouteroptions10) | 是    | 跳转页面描述信息。  |
748| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
749| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
750
751**错误码:**
752
753以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
754> **说明**:
755>
756> 该接口返回的以下错误码均为string类型。
757
758| 错误码ID   | 错误信息 |
759| --------- | ------- |
760| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
761| 100001    | Internal error. |
762| 100003    | Page stack error. Too many pages are pushed. |
763| 100004    | Named route error. The named route does not exist. |
764
765**示例:**
766
767```ts
768class innerParams {
769  data3: number[]
770
771  constructor(tuple: number[]) {
772    this.data3 = tuple
773  }
774}
775
776class routerParams {
777  data1: string
778  data2: innerParams
779
780  constructor(str: string, tuple: number[]) {
781    this.data1 = str
782    this.data2 = new innerParams(tuple)
783  }
784}
785
786router.pushNamedRoute({
787  name: 'myPage',
788  params: new routerParams('message', [123, 456, 789])
789}, router.RouterMode.Standard, (err) => {
790  if (err) {
791    console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`);
792    return;
793  }
794  console.info('pushNamedRoute success');
795})
796```
797
798## router.replaceNamedRoute<sup>10+</sup>
799
800replaceNamedRoute(options: NamedRouterOptions): Promise&lt;void&gt;
801
802用指定的命名路由页面替换当前页面,并销毁被替换的页面。
803
804**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
805
806**系统能力:** SystemCapability.ArkUI.ArkUI.Full
807
808**参数:**
809
810| 参数名  | 类型                            | 必填 | 说明               |
811| ------- | ------------------------------- | ---- | ------------------ |
812| options | [NamedRouterOptions](#namedrouteroptions10) | 是   | 替换页面描述信息。 |
813
814**返回值:**
815
816| 类型                | 说明        |
817| ------------------- | --------- |
818| Promise&lt;void&gt; | 异常返回结果。 |
819
820**错误码:**
821
822以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
823> **说明**:
824>
825> 该接口返回的以下错误码均为string类型。
826
827| 错误码ID   | 错误信息 |
828| --------- | ------- |
829| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
830| 100001    | The UI execution context is not found. This error code is thrown only in the standard system. |
831| 100004    | Named route error. The named route does not exist. |
832
833**示例:**
834
835```ts
836import { BusinessError } from '@kit.BasicServicesKit';
837
838class routerParams {
839  data1: string
840
841  constructor(str: string) {
842    this.data1 = str
843  }
844}
845
846router.replaceNamedRoute({
847  name: 'myPage',
848  params: new routerParams('message')
849})
850  .then(() => {
851    console.error(`replaceNamedRoute finish`);
852  })
853  .catch((err: ESObject) => {
854    console.error(`replaceNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
855  })
856```
857
858## router.replaceNamedRoute<sup>10+</sup>
859
860replaceNamedRoute(options: NamedRouterOptions, callback: AsyncCallback&lt;void&gt;): void
861
862用指定的命名路由页面替换当前页面,并销毁被替换的页面。
863
864**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
865
866**系统能力:** SystemCapability.ArkUI.ArkUI.Full
867
868**参数:**
869
870| 参数名  | 类型                            | 必填 | 说明               |
871| ------- | ------------------------------- | ---- | ------------------ |
872| options | [NamedRouterOptions](#namedrouteroptions10) | 是   | 替换页面描述信息。 |
873| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
874
875**错误码:**
876
877以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
878> **说明**:
879>
880> 该接口返回的以下错误码均为string类型。
881
882| 错误码ID   | 错误信息 |
883| --------- | ------- |
884| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
885| 100001    | The UI execution context is not found. This error code is thrown only in the standard system. |
886| 100004    | Named route error. The named route does not exist. |
887
888**示例:**
889
890```ts
891class routerParams {
892  data1: string
893
894  constructor(str: string) {
895    this.data1 = str
896  }
897}
898
899router.replaceNamedRoute({
900  name: 'myPage',
901  params: new routerParams('message')
902}, (err) => {
903  if (err) {
904    console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`);
905    return;
906  }
907  console.info('replaceNamedRoute success');
908})
909```
910
911## router.replaceNamedRoute<sup>10+</sup>
912
913replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise&lt;void&gt;
914
915用指定的命名路由页面替换当前页面,并销毁被替换的页面。
916
917**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
918
919**系统能力:** SystemCapability.ArkUI.ArkUI.Full
920
921**参数:**
922
923| 参数名     | 类型                              | 必填   | 说明         |
924| ------- | ------------------------------- | ---- | ---------- |
925| options | [NamedRouterOptions](#namedrouteroptions10) | 是    | 替换页面描述信息。  |
926| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
927
928
929**返回值:**
930
931| 类型                | 说明        |
932| ------------------- | --------- |
933| Promise&lt;void&gt; | 异常返回结果。 |
934
935**错误码:**
936
937以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
938> **说明**:
939>
940> 该接口返回的以下错误码均为string类型。
941
942| 错误码ID   | 错误信息 |
943| --------- | ------- |
944| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
945| 100001    | Failed to get the delegate. This error code is thrown only in the standard system. |
946| 100004    | Named route error. The named route does not exist. |
947
948**示例:**
949
950```ts
951import { BusinessError } from '@kit.BasicServicesKit';
952
953class routerParams {
954  data1: string
955
956  constructor(str: string) {
957    this.data1 = str
958  }
959}
960
961router.replaceNamedRoute({
962  name: 'myPage',
963  params: new routerParams('message')
964}, router.RouterMode.Standard)
965  .then(() => {
966    console.error(`replaceNamedRoute finish`);
967  })
968  .catch((err: ESObject) => {
969    console.error(`replaceNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
970  })
971```
972
973## router.replaceNamedRoute<sup>10+</sup>
974
975replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback&lt;void&gt;): void
976
977用指定的命名路由页面替换当前页面,并销毁被替换的页面。
978
979**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
980
981**系统能力:** SystemCapability.ArkUI.ArkUI.Full
982
983**参数:**
984
985| 参数名     | 类型                              | 必填   | 说明         |
986| ------- | ------------------------------- | ---- | ---------- |
987| options | [NamedRouterOptions](#namedrouteroptions10) | 是    | 替换页面描述信息。  |
988| mode    | [RouterMode](#routermode9)      | 是    | 跳转页面使用的模式。 |
989| callback | AsyncCallback&lt;void&gt;      | 是   | 异常响应回调。   |
990
991**错误码:**
992
993以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
994> **说明**:
995>
996> 该接口返回的以下错误码均为string类型。
997
998| 错误码ID   | 错误信息 |
999| --------- | ------- |
1000| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
1001| 100001    | The UI execution context is not found. This error code is thrown only in the standard system. |
1002| 100004    | Named route error. The named route does not exist. |
1003
1004**示例:**
1005
1006```ts
1007class routerParams {
1008  data1: string
1009
1010  constructor(str: string) {
1011    this.data1 = str
1012  }
1013}
1014
1015router.replaceNamedRoute({
1016  name: 'myPage',
1017  params: new routerParams('message')
1018}, router.RouterMode.Standard, (err) => {
1019  if (err) {
1020    console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`);
1021    return;
1022  }
1023  console.info('replaceNamedRoute success');
1024});
1025```
1026
1027## router.back
1028
1029back(options?: RouterOptions ): void
1030
1031返回上一页面或指定的页面,会删除当前页面与指定页面之间的所有页面。
1032
1033**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1034
1035**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1036
1037**参数:**
1038
1039| 参数名  | 类型                            | 必填 | 说明                                                         |
1040| ------- | ------------------------------- | ---- | ------------------------------------------------------------ |
1041| options | [RouterOptions](#routeroptions) | 否   | 返回页面描述信息,其中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不响应该情况。如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会回收,出栈后会被回收。back是返回接口,url设置为特殊值"/"不生效。如果是用命名路由的方式跳转,传入的url需是命名路由的名称。 |
1042
1043**示例:**
1044
1045```ts
1046router.back({ url: 'pages/detail' });
1047```
1048
1049## router.back<sup>12+</sup>
1050
1051back(index: number, params?: Object): void;
1052
1053返回指定的页面,会删除当前页面与指定页面之间的所有页面。
1054
1055**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
1056
1057**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1058
1059**参数:**
1060
1061| 参数名     | 类型                              | 必填   | 说明         |
1062| ------- | ------------------------------- | ---- | ---------- |
1063| index | number | 是    | 跳转目标页面的索引值。 从栈底到栈顶,index从1开始递增。 |
1064| params    | Object      | 否    | 页面返回时携带的参数。 |
1065
1066**示例:**
1067
1068```ts
1069router.back(1);
1070```
1071```ts
1072router.back(1, { info: '来自Home页' }); //携带参数返回
1073```
1074
1075## router.clear
1076
1077clear(): void
1078
1079清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。
1080
1081**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1082
1083**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1084
1085**示例:**
1086
1087```ts
1088router.clear();
1089```
1090
1091## router.getLength
1092
1093getLength(): string
1094
1095获取当前在页面栈内的页面数量。
1096
1097**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1098
1099**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1100
1101**返回值:**
1102
1103| 类型     | 说明                 |
1104| ------ | ------------------ |
1105| string | 页面数量,页面栈支持最大数值是32。 |
1106
1107**示例:**
1108
1109```ts
1110let size = router.getLength();
1111console.log('pages stack size = ' + size);
1112```
1113
1114## router.getState
1115
1116getState(): RouterState
1117
1118获取栈顶页面的状态信息。
1119
1120**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1121
1122**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1123
1124**返回值:**
1125
1126| 类型                          | 说明      |
1127| --------------------------- | ------- |
1128| [RouterState](#routerstate) | 页面状态信息。 |
1129
1130**示例:**
1131
1132```ts
1133let page = router.getState();
1134console.log('current index = ' + page.index);
1135console.log('current name = ' + page.name);
1136console.log('current path = ' + page.path);
1137```
1138
1139## router.getStateByIndex<sup>12+</sup>
1140
1141getStateByIndex(index: number): RouterState | undefined
1142
1143通过索引值获取对应页面的状态信息。
1144
1145**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
1146
1147**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1148
1149**参数:**
1150
1151| 参数名     | 类型                              | 必填   | 说明         |
1152| ------- | ------------------------------- | ---- | ---------- |
1153| index    | number | 是   | 表示要获取的页面索引。从栈底到栈顶,index从1开始递增。 |
1154
1155**返回值:**
1156
1157| 类型                          | 说明      |
1158| --------------------------- | ------- |
1159| [RouterState](#routerstate) \| undefined | 返回页面状态信息。索引不存在时返回undefined。 |
1160
1161**示例:**
1162
1163```ts
1164let options: router.RouterState | undefined = router.getStateByIndex(1);
1165if (options != undefined) {
1166  console.log('index = ' + options.index);
1167  console.log('name = ' + options.name);
1168  console.log('path = ' + options.path);
1169  console.log('params = ' + options.params);
1170}
1171```
1172## router.getStateByUrl<sup>12+</sup>
1173
1174getStateByUrl(url: string): Array&lt;RouterState&gt;
1175
1176通过url获取对应页面的状态信息。
1177
1178**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
1179
1180**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1181
1182**参数:**
1183
1184| 参数名     | 类型                              | 必填   | 说明         |
1185| ------- | ------------------------------- | ---- | ---------- |
1186| url    | string | 是   | 表示要获取对应页面信息的url。  |
1187
1188**返回值:**
1189
1190| 类型                          | 说明      |
1191| --------------------------- | ------- |
1192| Array<[RouterState](#routerstate)> | 页面状态信息。 |
1193
1194**示例:**
1195
1196```ts
1197let options: Array<router.RouterState> = router.getStateByUrl('pages/index');
1198for (let i: number = 0; i < options.length; i++) {
1199  console.log('index = ' + options[i].index);
1200  console.log('name = ' + options[i].name);
1201  console.log('path = ' + options[i].path);
1202  console.log('params = ' + options[i].params);
1203}
1204```
1205
1206## RouterState
1207
1208页面状态信息。
1209
1210**系统能力:** SystemCapability.ArkUI.ArkUI.Full1211
1212| 名称  | 类型   | 必填 | 说明                                                         |
1213| ----- | ------ | ---- | ------------------------------------------------------------ |
1214| index | number | 是   | 表示当前页面在页面栈中的索引。从栈底到栈顶,index从1开始递增。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1215| name  | string | 是  | 表示当前页面的名称,即对应文件名。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1216| path  | string | 是   | 表示当前页面的路径。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1217| params<sup>12+</sup>  | Object |  是  | 表示当前页面携带的参数。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。                                         |
1218
1219## router.showAlertBeforeBackPage<sup>9+</sup>
1220
1221showAlertBeforeBackPage(options: EnableAlertOptions): void
1222
1223开启页面返回询问对话框。
1224
1225**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1226
1227**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1228
1229**参数:**
1230
1231| 参数名     | 类型                                       | 必填   | 说明        |
1232| ------- | ---------------------------------------- | ---- | --------- |
1233| options | [EnableAlertOptions](#enablealertoptions) | 是    | 文本弹窗信息描述。 |
1234
1235**错误码:**
1236
1237以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。
1238
1239| 错误码ID   | 错误信息 |
1240| --------- | ------- |
1241| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
1242| 100001    | Internal error. |
1243
1244**示例:**
1245
1246```ts
1247import { BusinessError } from '@kit.BasicServicesKit';
1248
1249try {
1250  router.showAlertBeforeBackPage({
1251    message: 'Message Info'
1252  });
1253} catch (err) {
1254  console.error(`showAlertBeforeBackPage failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
1255}
1256```
1257## EnableAlertOptions
1258
1259页面返回询问对话框选项。
1260
1261**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1262
1263**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full1264
1265| 名称      | 类型     | 必填   | 说明       |
1266| ------- | ------ | ---- | -------- |
1267| message | string | 是    | 询问对话框内容。 |
1268
1269## router.hideAlertBeforeBackPage<sup>9+</sup>
1270
1271hideAlertBeforeBackPage(): void
1272
1273禁用页面返回询问对话框。
1274
1275**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1276
1277**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1278
1279**示例:**
1280
1281```ts
1282router.hideAlertBeforeBackPage();
1283```
1284
1285##  router.getParams
1286
1287getParams(): Object
1288
1289获取发起跳转的页面往当前页传入的参数。
1290
1291**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1292
1293**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1294
1295**返回值:**
1296
1297| 类型   | 说明                               |
1298| ------ | ---------------------------------- |
1299| object | 发起跳转的页面往当前页传入的参数。 |
1300
1301**示例:**
1302
1303```ts
1304router.getParams();
1305```
1306
1307## RouterOptions
1308
1309路由跳转选项。
1310
1311**系统能力:** SystemCapability.ArkUI.ArkUI.Lite1312
1313| 名称   | 类型   | 必填 | 说明                                                         |
1314| ------ | ------ | ---- | ------------------------------------------------------------ |
1315| url    | string | 是   | 表示目标页面的url,可以用以下两种格式:<br/>-&nbsp;页面绝对路径,由配置文件中pages列表提供,例如:<br/>&nbsp;&nbsp;-&nbsp;pages/index/index<br/>&nbsp;&nbsp;-&nbsp;pages/detail/detail<br/>-&nbsp;特殊值,如果url的值是"/",则跳转到首页,首页默认为页面跳转配置项src数组的第一个数据项。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1316| params | Object | 否   | 表示路由跳转时要同时传递到目标页面的数据,切换到其他页面时,当前接收的数据失效。跳转到目标页面后,使用router.getParams()获取传递的参数,此外,在类web范式中,参数也可以在页面中直接使用,如this.keyValue(keyValue为跳转时params参数中的key值),如果目标页面中已有该字段,则其值会被传入的字段值覆盖。<br/>**说明:** <br/>params参数不能传递方法和系统接口返回的对象(例如,媒体接口定义和返回的PixelMap对象)。建议开发者提取系统接口返回的对象中需要被传递的基础类型属性,自行构造object类型对象进行传递。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1317| recoverable<sup>14+</sup> | boolean | 否   | 表示对应的页面是否可恢复,默认为true,表示可恢复。<br/>**说明:** <br/> 当应用退到后台,并且在未来的某个时间点,由于系统资源限制等原因被系统杀死,如果某个页面被设置成可恢复,那么该应用再次被拉到前台后系统可以恢复出页面,详细说明请参考[UIAbility备份恢复](../../application-models/ability-recover-guideline.md)。 |
1318
1319  > **说明:**
1320  > 页面路由栈支持的最大Page数量为32。
1321
1322## RouterMode<sup>9+</sup>
1323
1324路由跳转模式。
1325
1326**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1327
1328**系统能力:** SystemCapability.ArkUI.ArkUI.Full1329
1330| 名称     | 值 | 说明                                                         |
1331| -------- | --- | ------------------------------------------------------------ |
1332| Standard | 0 | 多实例模式,也是默认情况下的跳转模式。 <br/>目标页面会被添加到页面栈顶,无论栈中是否存在相同url的页面。<br/>**说明:**  <br/>不使用路由跳转模式时,则按照默认的多实例模式进行跳转。 |
1333| Single   | 1 | 单实例模式。<br/>如果目标页面的url已经存在于页面栈中,则该url页面移动到栈顶。<br />如果目标页面的url在页面栈中不存在同url页面,则按照默认的多实例模式进行跳转。 |
1334
1335## NamedRouterOptions<sup>10+</sup>
1336
1337命名路由跳转选项。
1338
1339| 名称   | 类型   | 必填 | 说明                                                         |
1340| ------ | ------ | ---- | ------------------------------------------------------------ |
1341| name   | string | 是   | 表示目标命名路由页面的name。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 <br/>**系统能力:** SystemCapability.ArkUI.ArkUI.Full |
1342| params | Object | 否   | 表示路由跳转时要同时传递到目标页面的数据。跳转到目标页面后,使用router.getParams()获取传递的参数,此外,在类web范式中,参数也可以在页面中直接使用,如this.keyValue(keyValue为跳转时params参数中的key值),如果目标页面中已有该字段,则其值会被传入的字段值覆盖。 <br/>**说明:** <br/>params参数不能传递方法和系统接口返回的对象(例如,媒体接口定义和返回的PixelMap对象)。建议开发者提取系统接口返回的对象中需要被传递的基础类型属性,自行构造object类型对象进行传递。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。<br/>**系统能力:** SystemCapability.ArkUI.ArkUI.Full  |
1343| recoverable<sup>14+</sup> | boolean | 否   | 表示对应的页面是否可恢复,默认为true,表示可恢复。<br/>**说明:** <br/> 当应用退到后台,并且在未来的某个时间点,由于系统资源限制等原因被系统杀死,如果某个页面被设置成可恢复,那么该应用再次被拉到前台后系统可以恢复出页面,详细说明请参考[UIAbility备份恢复](../../application-models/ability-recover-guideline.md)。 <br/>**系统能力:** SystemCapability.ArkUI.ArkUI.Lite |
1344
1345## 完整示例
1346
1347### 基于JS扩展的类Web开发范式
1348
1349以下代码仅适用于javascript文件,不适用于ArkTS文件
1350
1351<!--code_no_check-->
1352
1353```js
1354// 在当前页面中
1355export default {
1356  pushPage() {
1357    router.pushUrl({
1358      url: 'pages/detail/detail',
1359      params: {
1360        data1: 'message'
1361      }
1362    });
1363  }
1364}
1365```
1366<!--code_no_check-->
1367
1368```js
1369// 在detail页面中
1370export default {
1371  onInit() {
1372    console.info('showData1:' + router.getParams()['data1']);
1373  }
1374}
1375```
1376
1377### 基于TS扩展的声明式开发范式
1378
1379> **说明:**
1380>
1381> 直接使用router可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getRouter](js-apis-arkui-UIContext.md#getrouter)获取绑定实例的router。
1382
1383```ts
1384// 通过router.pushUrl跳转至目标页携带params参数
1385import { router } from '@kit.ArkUI';
1386import { BusinessError } from '@kit.BasicServicesKit'
1387
1388// 定义传递参数的类
1389class innerParams {
1390  array: number[]
1391
1392  constructor(tuple: number[]) {
1393    this.array = tuple
1394  }
1395}
1396
1397class routerParams {
1398  text: string
1399  data: innerParams
1400
1401  constructor(str: string, tuple: number[]) {
1402    this.text = str
1403    this.data = new innerParams(tuple)
1404  }
1405}
1406
1407@Entry
1408@Component
1409struct Index {
1410  async routePage() {
1411    let options: router.RouterOptions = {
1412      url: 'pages/second',
1413      params: new routerParams('这是第一页的值', [12, 45, 78])
1414    }
1415    // 建议使用this.getUIContext().getRouter().pushUrl()
1416    router.pushUrl(options)
1417      .then(() => {
1418        console.error(`pushUrl finish`);
1419      })
1420      .catch((err: ESObject) => {
1421        console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
1422      })
1423    }
1424
1425  build() {
1426    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
1427      Text('这是第一页')
1428        .fontSize(50)
1429        .fontWeight(FontWeight.Bold)
1430      Button() {
1431        Text('next page')
1432          .fontSize(25)
1433          .fontWeight(FontWeight.Bold)
1434      }.type(ButtonType.Capsule)
1435      .margin({ top: 20 })
1436      .backgroundColor('#ccc')
1437      .onClick(() => {
1438        this.routePage()
1439      })
1440    }
1441    .width('100%')
1442    .height('100%')
1443  }
1444}
1445```
1446
1447```ts
1448// 在second页面中接收传递过来的参数
1449import { router } from '@kit.ArkUI';
1450
1451class innerParams {
1452  array: number[]
1453
1454  constructor(tuple: number[]) {
1455    this.array = tuple
1456  }
1457}
1458
1459class routerParams {
1460  text: string
1461  data: innerParams
1462
1463  constructor(str: string, tuple: number[]) {
1464    this.text = str
1465    this.data = new innerParams(tuple)
1466  }
1467}
1468
1469@Entry
1470@Component
1471struct Second {
1472  private content: string = "这是第二页"
1473  // 建议使用this.getUIContext().getRouter().getParams()
1474  @State text: string = (router.getParams() as routerParams).text
1475  @State data: object = (router.getParams() as routerParams).data
1476  @State secondData: string = ''
1477
1478  build() {
1479    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
1480      Text(`${this.content}`)
1481        .fontSize(50)
1482        .fontWeight(FontWeight.Bold)
1483      Text(this.text)
1484        .fontSize(30)
1485        .onClick(() => {
1486          this.secondData = (this.data['array'][1]).toString()
1487        })
1488        .margin({ top: 20 })
1489      Text(`第一页传来的数值:${this.secondData}`)
1490        .fontSize(20)
1491        .margin({ top: 20 })
1492        .backgroundColor('red')
1493    }
1494    .width('100%')
1495    .height('100%')
1496  }
1497}
1498```
1499
1500## router.push<sup>(deprecated)</sup>
1501
1502push(options: RouterOptions): void
1503
1504跳转到应用内的指定页面。
1505
1506从API version9开始不再维护,建议使用[pushUrl<sup>9+</sup>](#routerpushurl9)
1507
1508**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1509
1510**参数:**
1511
1512| 参数名     | 类型                              | 必填   | 说明        |
1513| ------- | ------------------------------- | ---- | --------- |
1514| options | [RouterOptions](#routeroptions) | 是    | 跳转页面描述信息。 |
1515
1516
1517**示例:**
1518
1519```ts
1520class innerParams {
1521  data3: number[]
1522
1523  constructor(tuple: number[]) {
1524    this.data3 = tuple
1525  }
1526}
1527
1528class routerParams {
1529  data1: string
1530  data2: innerParams
1531
1532  constructor(str: string, tuple: number[]) {
1533    this.data1 = str
1534    this.data2 = new innerParams(tuple)
1535  }
1536}
1537
1538router.push({
1539  url: 'pages/routerpage2',
1540  params: new routerParams('message', [123, 456, 789])
1541});
1542```
1543
1544## router.replace<sup>(deprecated)</sup>
1545
1546replace(options: RouterOptions): void
1547
1548用应用内的某个页面替换当前页面,并销毁被替换的页面。
1549
1550从API version9开始不再维护,建议使用[replaceUrl<sup>9+</sup>](#routerreplaceurl9)
1551
1552**系统能力:** SystemCapability.ArkUI.ArkUI.Lite
1553
1554**参数:**
1555
1556| 参数名  | 类型                            | 必填 | 说明               |
1557| ------- | ------------------------------- | ---- | ------------------ |
1558| options | [RouterOptions](#routeroptions) | 是   | 替换页面描述信息。 |
1559
1560**示例:**
1561
1562```ts
1563class routerParams {
1564  data1: string
1565
1566  constructor(str: string) {
1567    this.data1 = str
1568  }
1569}
1570
1571router.replace({
1572  url: 'pages/detail',
1573  params: new routerParams('message')
1574});
1575```
1576
1577## router.enableAlertBeforeBackPage<sup>(deprecated)</sup>
1578
1579enableAlertBeforeBackPage(options: EnableAlertOptions): void
1580
1581开启页面返回询问对话框。
1582
1583从API version9开始不再维护,建议使用[showAlertBeforeBackPage<sup>9+</sup>](#routershowalertbeforebackpage9)
1584
1585**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1586
1587**参数:**
1588
1589| 参数名     | 类型                                       | 必填   | 说明        |
1590| ------- | ---------------------------------------- | ---- | --------- |
1591| options | [EnableAlertOptions](#enablealertoptions) | 是    | 文本弹窗信息描述。 |
1592
1593**示例:**
1594
1595```ts
1596router.enableAlertBeforeBackPage({
1597  message: 'Message Info'
1598});
1599```
1600
1601## router.disableAlertBeforeBackPage<sup>(deprecated)</sup>
1602
1603disableAlertBeforeBackPage(): void
1604
1605禁用页面返回询问对话框。
1606
1607从API version9开始不再维护,建议使用[hideAlertBeforeBackPage<sup>9+</sup>](#routerhidealertbeforebackpage9)
1608
1609**系统能力:** SystemCapability.ArkUI.ArkUI.Full
1610
1611**示例:**
1612
1613```ts
1614router.disableAlertBeforeBackPage();
1615```