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.
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.
...
@@ -40,9 +40,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -40,9 +40,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found. |
| 100002 | Uri error. The uri of router is not exist. |
| 100002 | If the uri is not exist. |
| 100003 | Page stack error. The pages are pushed too much. |
| 100003 | If the pages are pushed too much. |
**Example**
**Example**
...
@@ -54,8 +54,8 @@ try {
...
@@ -54,8 +54,8 @@ try {
data1:'message',
data1:'message',
data2:{
data2:{
data3:[123,456,789]
data3:[123,456,789]
},
}
},
}
})
})
.then(()=>{
.then(()=>{
// success
// success
...
@@ -89,9 +89,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -89,9 +89,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found. |
| 100002 | Uri error. The uri of router is not exist. |
| 100002 | If the uri is not exist. |
| 100003 | Page stack error. The pages are pushed too much. |
| 100003 | If the pages are pushed too much. |
**Example**
**Example**
...
@@ -103,8 +103,8 @@ try {
...
@@ -103,8 +103,8 @@ try {
data1:'message',
data1:'message',
data2:{
data2:{
data3:[123,456,789]
data3:[123,456,789]
},
}
},
}
},(err)=>{
},(err)=>{
if(err){
if(err){
console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
...
@@ -143,9 +143,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -143,9 +143,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found. |
| 100002 | Uri error. The uri of router is not exist. |
| 100002 | If the uri is not exist. |
| 100003 | Page stack error. The pages are pushed too much. |
| 100003 | If the pages are pushed too much. |
**Example**
**Example**
...
@@ -157,8 +157,8 @@ try {
...
@@ -157,8 +157,8 @@ try {
data1:'message',
data1:'message',
data2:{
data2:{
data3:[123,456,789]
data3:[123,456,789]
},
}
},
}
},router.RouterMode.Standard)
},router.RouterMode.Standard)
.then(()=>{
.then(()=>{
// success
// success
...
@@ -193,9 +193,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -193,9 +193,9 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found. |
| 100002 | Uri error. The uri of router is not exist. |
| 100002 | If the uri is not exist. |
| 100003 | Page stack error. The pages are pushed too much. |
| 100003 | If the pages are pushed too much. |
**Example**
**Example**
...
@@ -207,8 +207,8 @@ try {
...
@@ -207,8 +207,8 @@ try {
data1:'message',
data1:'message',
data2:{
data2:{
data3:[123,456,789]
data3:[123,456,789]
},
}
},
}
},router.RouterMode.Standard,(err)=>{
},router.RouterMode.Standard,(err)=>{
if(err){
if(err){
console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
...
@@ -247,8 +247,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -247,8 +247,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found, only throw in standard system. |
| 200002 | Uri error. The uri of router is not exist. |
| 200002 | If the uri is not exist. |
**Example**
**Example**
...
@@ -257,8 +257,8 @@ try {
...
@@ -257,8 +257,8 @@ try {
router.replaceUrl({
router.replaceUrl({
url:'pages/detail',
url:'pages/detail',
params:{
params:{
data1:'message',
data1:'message'
},
}
})
})
.then(()=>{
.then(()=>{
// success
// success
...
@@ -292,8 +292,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -292,8 +292,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found, only throw in standard system. |
| 200002 | Uri error. The uri of router is not exist. |
| 200002 | If the uri is not exist. |
**Example**
**Example**
...
@@ -302,8 +302,8 @@ try {
...
@@ -302,8 +302,8 @@ try {
router.replaceUrl({
router.replaceUrl({
url:'pages/detail',
url:'pages/detail',
params:{
params:{
data1:'message',
data1:'message'
},
}
},(err)=>{
},(err)=>{
if(err){
if(err){
console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
...
@@ -344,8 +344,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -344,8 +344,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found, only throw in standard system. |
| 200002 | Uri error. The uri of router is not exist. |
| 200002 | If the uri is not exist. |
**Example**
**Example**
...
@@ -354,8 +354,8 @@ try {
...
@@ -354,8 +354,8 @@ try {
router.replaceUrl({
router.replaceUrl({
url:'pages/detail',
url:'pages/detail',
params:{
params:{
data1:'message',
data1:'message'
},
}
},router.RouterMode.Standard)
},router.RouterMode.Standard)
.then(()=>{
.then(()=>{
// success
// success
...
@@ -390,8 +390,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
...
@@ -390,8 +390,8 @@ For details about the error codes, see [Router Error Codes](../errorcodes/errorc
| ID | Error Message|
| ID | Error Message|
| --------- | ------- |
| --------- | ------- |
| 100001 | Internal error. |
| 100001 | If UI execution context not found, only throw in standard system. |
| 200002 | Uri error. The uri of router is not exist. |
| 200002 | If the uri is not exist. |
**Example**
**Example**
...
@@ -400,8 +400,8 @@ try {
...
@@ -400,8 +400,8 @@ try {
router.replaceUrl({
router.replaceUrl({
url:'pages/detail',
url:'pages/detail',
params:{
params:{
data1:'message',
data1:'message'
},
}
},router.RouterMode.Standard,(err)=>{
},router.RouterMode.Standard,(err)=>{
if(err){
if(err){
console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
...
@@ -465,7 +465,7 @@ Obtains the number of pages in the current stack.
...
@@ -465,7 +465,7 @@ Obtains the number of pages in the current stack.
**Example**
**Example**
```js
```js
varsize=router.getLength();
letsize=router.getLength();
console.log('pages stack size = '+size);
console.log('pages stack size = '+size);
```
```
...
@@ -486,7 +486,7 @@ Obtains state information about the current page.
...
@@ -486,7 +486,7 @@ Obtains state information about the current page.
**Example**
**Example**
```js
```js
varpage=router.getState();
letpage=router.getState();
console.log('current index = '+page.index);
console.log('current index = '+page.index);
console.log('current name = '+page.name);
console.log('current name = '+page.name);
console.log('current path = '+page.path);
console.log('current path = '+page.path);
...
@@ -498,11 +498,11 @@ Describes the page routing state.
...
@@ -498,11 +498,11 @@ Describes the page routing state.
| url | string | Yes | URL of the target 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 URL is a slash (/), the home page is displayed.|
| url | string | Yes | URL of the target 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 URL is a slash (/), the home page is displayed.|
| 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.|
| 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.|
| Standard | Standard mode.<br>The target page is added to the top of the page stack, regardless of whether a page with the same URL exists in the stack.|
| Standard | Standard mode.<br>The target page is added to the top of the page stack, regardless of whether a page with the same URL exists in the stack.|
| 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.|
| 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
## Examples
...
@@ -618,8 +618,8 @@ export default {
...
@@ -618,8 +618,8 @@ export default {
router.push({
router.push({
url:'pages/detail/detail',
url:'pages/detail/detail',
params:{
params:{
data1:'message',
data1:'message'
},
}
});
});
}
}
}
}
...
@@ -642,18 +642,18 @@ import router from '@ohos.router'
...
@@ -642,18 +642,18 @@ import router from '@ohos.router'
// To arrange the child components from left to right, set direction of the parent container to Direction.Auto or Direction.Ltr, or leave it to the default value.
// To arrange the child components from left to right, set direction of the parent container to Direction.Ltr.