The **Router** module provides APIs to access pages through URLs. You can use the APIs to navigate to a specified page in an application, replace the current page with another one in an application, and return to the previous page or a specified page.
> **NOTE**
>
> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - Page routing APIs can be invoked only after page rendering is complete. Do not call the APIs in **onInit** and **onReady** when the page is still in the rendering phase.
>
> - Page routing APIs can be invoked only after page rendering is complete. Do not call these APIs in **onInit** and **onReady** when the page is still in the rendering phase.
## Modules to Import
...
...
@@ -11,10 +14,6 @@
import router from '@ohos.router'
```
## Required Permissions
None.
## router.push
push(options: RouterOptions): void
...
...
@@ -24,84 +23,98 @@ Navigates to a specified page in the application.
| options | [RouterOptions](#routeroptions) | Yes| Description of the page. The **url** parameter indicates the URL of the page to return to. If the specified page does not exist in the page stack, the application does not respond. If this parameter is not set, the application returns to the previous page.|
| options | [RouterOptions](#routeroptions) | No | Description of the page. The **url** parameter indicates the URL of the page to return to. If the specified page does not exist in the page stack, the application does not respond. If this parameter is not set, the application returns to the previous page.|
**Example**
```js
// index page
exportdefault{
indexPushPage(){
router.push({
url:'pages/detail/detail',
});
}
}
```
```js
// detail page
exportdefault{
detailPushPage(){
router.push({
url:'pages/mall/mall',
});
}
}
```
```js
// Navigate from the mall page to the detail page through router.back().
exportdefault{
mallBackPage(){
router.back();
}
}
```
```js
// Navigate from the detail page to the index page through router.back().
exportdefault{
defaultBack(){
router.back();
}
}
```
```js
// Return to the detail page through router.back().
exportdefault{
backToDetail(){
router.back({uri:'pages/detail/detail'});
}
}
router.back({uri:'pages/detail'});
```
## router.clear
...
...
@@ -176,12 +145,9 @@ Clears all historical pages in the stack and retains only the current page at th
| index | number | Index of the current page in the stack.<br>> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>> The index starts from 1 from the bottom to the top of the stack.|
| name | string | Name of the current page, that is, the file name.|
| url | string | Yes | URI of the destination page, in either of the following formats:<br>- Absolute path of the page. The value is available in the pages list in the **config.json** file, for example:<br>- pages/index/index<br>- pages/detail/detail<br>- Particular path. If the URI is a slash (/), the home page is displayed. |
| 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.|
> **NOTE**
>
> The page routing stack supports a maximum of 32 pages.
| Single | Singleton mode.<br>If the URL of the target page already exists in the page stack, the page closest to the top of the stack is moved as a new page to the top of the stack.<br>If the URL of the target page does not exist in the page stack, the page is redirected to in standard mode.|
## Examples
### JavaScript-based Web-like Development Paradigm
```js
// Current page
exportdefault{
pushPage(){
router.push({
url:'pages/detail/detail',
...
...
@@ -328,26 +308,26 @@ Obtains the parameters passed from the page that initiates redirection to the cu
| url | string | Yes| URI of the destination page, in either of the following formats:<br>- Absolute path of the page. The value is available in the pages list in the config.json file, for example:<br> - pages/index/index<br> - pages/detail/detail<br>- Particular path. If the URI is a slash (/), the home page is displayed.|
| 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.|