has.switchTab(Object object)
Switches between different tabs within the app. If an app defines multiple tabs, that is, there are tabs at the bottom or top of the client window, this API can be used to redirect to the tabBar page and close all other non-tabBar pages.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| url | string | Yes | Path of the tabBar page (defined in the tabbar field in app.json) to redirect to. The path cannot contain parameters. For example, if page/tab1/tab1 is defined in the tabbar field in app.json, the following formats are supported: 1. Global absolute path starting with a slash (/): for example, /page/tab1/tab1. 2. Relative paths: for example, tab1, ./tab1, and ../tab1. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: 1.url is invalid. 2.current path error. |
| 20000201 | switchTab error. |
Example:
Define tabbar in the app.json file.
"tabBar": {
"list": [
{
"pagePath": "page/tabbar1/tabbar1",
"text": "tabbar1",
"iconPath": "image/icon_tabbar1.png",
"selectedIconPath": "image/icon_tabbar1_HL.png"
},
{
"pagePath": "page/tabbar2/tabbar2",
"iconPath": "image/icon_tabbar2.png",
"selectedIconPath": "image/icon_tabbar1_HL.png",
"text": "tabbar2"
}
]
} Call the following API:
has.switchTab({
url: '/page/tabbar1/tabbar1',
success: (res) => {
console.info('switchTab success', res);
},
fail: (err) => {
console.error('switchTab fail', err);
},
complete: (res) => {
console.info('switchTab complete', res);
}
}); has.reLaunch(Object object)
Closes all current pages and opens an in-app page.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| url | string | Yes | Path to the in-app page to open (that is, the code package path defined in app.json). Parameters can be appended to the path in the following format: a question mark (?) between a parameter and the path; an equal sign (=) between a parameter and its value; ampersands (&) between multiple parameters. Supported formats (assuming a page is defined as page/path/path in app.json): 1. Global absolute path starting with a slash (/). 2. Relative paths: for example, a.png, ./a.png, and ../a.png. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: gotoPage fetchModule fail. |
| 102 | Parameter error. Possible causes: 1.url is invalid. 2.current path error. 3.page not found. |
| 20000501 | reLaunch error. |
Example:
Call the following API:
has.reLaunch({
url: '/page/path/path?key=1',
success: (res) => {
console.info('reLaunch success', res);
},
fail: (err) => {
console.error('reLaunch fail', err);
},
complete: (res) => {
console.info('reLaunch complete', res);
}
}); On the /page/path page, options contains the parameters appended to the path.
Page({
onLoad: (options) => {
console.info(options);
}
}) has.redirectTo(Object object)
Redirects to an in-app page while closing the current page. This API cannot navigate to the tabBar page.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| url | string | Yes | Path to the in-app non-tabBar page to open (that is, the code package path defined in app.json). Parameters can be appended to the path in the following format: a question mark (?) between a parameter and the path; an equal sign (=) between a parameter and its value; ampersands (&) between multiple parameters. Supported formats (assuming a page is defined as page/path/path in app.json): 1. Global absolute path starting with a slash (/). 2. Relative paths: for example, a.png, ./a.png, and ../a.png. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: gotoPage fetchModule fail. |
| 102 | Parameter error. Possible causes: 1.url is invalid. 2.current path error. 3.tabBar url not allowed. 4.page not found. |
| 20000301 | redirectTo error. |
Example:
has.redirectTo({
url: '/page/path/path?key=1',
success: (res) => {
console.info('redirectTo success', res);
},
fail: (err) => {
console.error('redirectTo fail', err);
},
complete: (res) => {
console.info('redirectTo complete', res);
}
}); EventChannel.emit(string eventName, Object|Array|string args)
Triggers an event.
Since: 1.0.21
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| eventName | string | Yes | Event name. |
| args | Object | Array | string | No | Event parameter. |
EventChannel.on(string eventName, function fn)
Continuously listens for an event.
Since: 1.0.21
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| eventName | string | Yes | Event name. |
| fn | function | Yes | Event listener function. |
EventChannel.once(string eventName, function fn)
Listens for an event once and becomes invalid after being triggered.
Since: 1.0.21
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| eventName | string | Yes | Event name. |
| fn | function | Yes | Event listener function. |
EventChannel.off(string eventName, function fn)
Cancels listening for an event. If the second parameter is provided, only the specified listener function is canceled. Otherwise, all listener functions are canceled.
Since: 1.0.21
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| eventName | string | Yes | Event name. |
| fn | function | No | Event listener function. |