目录

Router

has.switchTab

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

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

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);
  }
});

has.navigateTo

has.navigateTo(Object object)

Navigates to a page within the app while retaining the current page. This API cannot navigate to the tabBar page. You can use has.navigateBack to return to the original 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.

events Object No

Inter-page communication API, which is used to listen for data sent from an opened page to the current page.

Since: 1.0.21

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.

Return value of success

Parameter Type Mandatory Description
eventChannel EventChannel Yes

Communication with the opened page.

Since: 1.0.21

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.

20000101 navigateTo error.

Example:

// Page A
Page({
    navigateToEvent() {
      has.navigateTo({
        url: 'xxx', // Enter a correct path.
        events: {
          // Add a listener for a specified event to obtain the data sent from the opened page B to the current page.
          acceptDataFromOpenedPage: (data) => {
            // Obtain the data sent from the opened page B to the current page.
            console.info('acceptDataFromOpenedPage message', data);
          },
          // Listen for the someEvent event emitted by page B.
          someEvent: (data) => {
            // Receive another message from the opened page B.
            console.info('someEvent message', data);
          }
        },
        success: (res) => {
          console.info('navigateTo success', res);
          // Send data to the opened page B through eventChannel.
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' });
        },
        fail: (err) => {
          console.error('navigateTo fail', err);
        },
        complete: (res) => {
          console.info('navigateTo complete', res);
        }
      });
    }
  });

// Page B
Page({
    onLoad: () => {
      const eventChannel = this.getOpenerEventChannel();
      // Send data to the previous page A.
      eventChannel.emit('acceptDataFromOpenedPage', {data: 'test'});
      eventChannel.emit('someEvent', {data: 'test'});
      // Listen for the acceptDataFromOpenerPage event and obtain the data sent from page A to page B through the eventChannel.
      eventChannel.on('acceptDataFromOpenerPage', (data) => {
        console.info('eventChannel.on', data);
      });
    },
    onUnload: () => {
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.off('acceptDataFromOpenerPage');
    }
  });

EventChannel

Page event communication channel.

Since: 1.0.21

[h2]EventChannel.emit

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.

[h2]EventChannel.on

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.

[h2]EventChannel.once

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.

[h2]EventChannel.off

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.

has.navigateBack

has.navigateBack(Object object)

Closes the current page and navigates back to the previous page or multiple levels up in the page stack.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
delta number 1 No Number of navigation levels to go back. If delta exceeds the current depth of the navigation stack, the home page is returned.
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
20000401 navigateBack error.

Example: