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