目录

Application-Level Events

has.onAppShow

has.onAppShow(function callback)

Listens for the event indicating that the atomic service transitions to the foreground. The callback time of this event is the same as that of App.onShow. This API uses a callback to return the result.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating that the atomic service transitions to the foreground.

Return value of callback

An object containing the following fields.

Parameter Type Description
path string Path for starting the atomic service page.
scene string Scene.
query Object Parameters for starting the atomic service page.
referrerInfo Object Source information.

referrerInfo parameters

Parameter Type Description
packageName string Package name of the source application or atomic service.
extraData Object Parameters passed by the source application or atomic service.

Example:

has.onAppShow((res) => {
  console.info('onAppShow callback triggered', res);
});

has.onAppHide

has.onAppHide(function callback)

Listens for the event indicating that the atomic service transitions to the background. The callback time of this event is the same as that of App.onHide.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating that the atomic service transitions to the background.

Example:

has.onAppHide(() => {
  console.info('onAppHide callback triggered');
});

has.onPageNotFound

has.onPageNotFound(function callback)

Listens for the event indicating that the page to be opened by the atomic service does not exist. The callback time of this event is the same as that of App.onPageNotFound.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating that the page to be opened by the atomic service does not exist.

Return value of callback

An object containing the following fields.

Parameter Type Description
path string Path of the page that does not exist, which is the code package path.
isEntryPage boolean Whether this is the first page of the current launch. For example, in the case of access via a shared link, the first page is the share page that you configure.

Example:

has.onPageNotFound((res) => {
  console.info('onPageNotFound callback triggered', res);
});

has.onError

has.onError(function callback)

Listens for atomic service error events, for example, a script error or an API call error. The callback time and parameters of this event are the same as those of App.onError.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating an atomic service error.

Example:

const callback = (err) => {
  console.error('onError callback triggered', err);
};

has.onError(callback);

has.onLazyLoadError

has.onLazyLoadError(function callback)

Listens for events indicating that the atomic service fails to load an asynchronous component.

  • Asynchronous component loading usually requires subpackages to be downloaded. If the download times out, the callback with errMsg set to loadSubpackage: timeout is triggered. The default timeout is 5 seconds.

  • If the download fails, the callback with errMsg set to loadSubpackage: fail is triggered.

  • If this API is used on a page to listen for the download result, ensure that you manually call offLazyLoadError to cancel the listening when necessary to prevent unexpected memory leakage.

Since: 1.0.8

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating an asynchronous component loading failure.

Return value of callback

An object containing the following fields.

Parameter Type Description
type string Failure type. Currently, only 'subpackage' is supported.
subpackage string Subpackage to which the asynchronous component belongs.
errMsg string Details.

Enumerated values of task status in errMsg

Name Value Description
CREATE_TASK_FAILED -4 Failed to create a download task.
HIGHER_VERSION_INSTALLED -3 The same or a later version already exists on the user's device.
TASK_ALREADY_EXISTS -2 The download task already exists.
TASK_UNFOUND -1 No download task exists.
DOWNLOAD_PAUSING 2 The download task is paused.
DOWNLOAD_FAILED 5 Download failed.
DOWNLOAD_WAIT_WIFI 6 Downloads are allowed only over Wi-Fi. Currently, mobile data is being used, so users need to wait for a Wi-Fi connection.
INSTALL_FAILED 23 Installation failed.

Enumerated values of error code in errMsg

Name Value Description
MODULE_ALREADY_EXISTS -8 The module already exists.
MODULE_UNAVAILABLE -7 The module to be downloaded does not exist or does not match the device.
INVALID_REQUEST -6 The request is invalid because it contains inaccurate information.
NETWORK_ERROR -5 The request fails due to a network error.
INVOKER_VERIFICATION_FAILED -4 The caller information is abnormal.
FOREGROUND_REQUIRED -3 On-demand loading is allowed only when the app is running in the foreground.
ACTIVE_SESSION_LIMIT_EXCEEDED -2 The request is denied because at least one download task is in progress.
FAILURE -1 Failed.
DOWNLOAD_WAIT_WIFI 1 Downloads are allowed only over Wi-Fi. Currently, mobile data is being used, so users need to wait for a Wi-Fi connection.

Example:

const callback = (err) => {
  console.error('onLazyLoadError callback triggered', err);
};

has.onLazyLoadError(callback);

has.offPageNotFound

has.offPageNotFound(function callback)

Disables the listening for the event indicating that the page to be opened by the atomic service does not exist.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onPageNotFound. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = (res) => {
  console.info('onPageNotFound callback triggered', res);
};
has.onPageNotFound(callback);
has.offPageNotFound(callback);

has.offError

has.offError(function callback)

Disables listening for atomic service error events.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onError. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = (err) => {
  console.error('onError callback triggered', err);
};

has.onError(callback);
has.offError(callback);

has.offAppShow

has.offAppShow(function callback)

Disables listening for the event indicating that the atomic service transitions to the foreground.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onAppShow. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = (res) => {
  console.info('onAppShow callback triggered', res);
};
has.onAppShow(callback);
has.offAppShow(callback);

has.offAppHide

has.offAppHide(function callback)

Disables listening for the event indicating that the atomic service transitions to the background.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onAppHide. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = () => {
  console.info('onAppHide callback triggered');
};
has.onAppHide(callback);
has.offAppHide(callback);

has.offLazyLoadError

has.offLazyLoadError(function callback)

Disables listening for events indicating that the atomic service fails to load an asynchronous component.

Since: 1.0.8

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onLazyLoadError. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = (err) => {
  console.error('onLazyLoadError callback triggered', err);
};

has.onLazyLoadError(callback);
has.offLazyLoadError(callback);

has.onAudioInterruptionBegin

has.onAudioInterruptionBegin(function callback)

Listens for the event indicating that an audio clip is interrupted due to system occupation (for example, when an alarm goes off or an incoming call is received).

After this event is triggered, all audio clips in the atomic service will be paused.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating an audio clip is interrupted due to system occupation.

Example:

has.onAudioInterruptionBegin(() => {
  console.info('onAudioInterruptionBegin callback triggered');
});

has.onAudioInterruptionEnd

has.onAudioInterruptionEnd(function callback)

Listens for the event indicating an audio clip resumes playback after system occupation.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Callback for the event indicating an audio clip resumes playback after system occupation.

Example:

has.onAudioInterruptionEnd(() => {
  console.info('onAudioInterruptionEnd callback triggered');
});

has.offAudioInterruptionBegin

has.offAudioInterruptionBegin(function callback)

Disables listening for the event indicating that an audio clip is interrupted due to system occupation.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onAudioInterruptionBegin. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = () => {
  console.info('onAudioInterruptionBegin callback triggered');
};
has.onAudioInterruptionBegin(callback);
has.offAudioInterruptionBegin(callback);

has.offAudioInterruptionEnd

has.offAudioInterruptionEnd(function callback)

Disables listening for the event indicating that an audio clip resumes playback after system occupation.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onAudioInterruptionEnd. If this parameter is not passed, all listener functions will be removed.

Example:

const callback = () => {
  console.info('onAudioInterruptionEnd callback triggered');
};
has.onAudioInterruptionEnd(callback);
has.offAudioInterruptionEnd(callback);