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