@@ -9,10 +9,10 @@ The **EventHub** module provides APIs to subscribe to, unsubscribe from, and tri
...
@@ -9,10 +9,10 @@ The **EventHub** module provides APIs to subscribe to, unsubscribe from, and tri
## Usage
## Usage
Before using any APIs in the **EventHub**, you must obtain an **EventHub** instance through the member variable **context** of the **Ability** instance.
Before using any APIs in the **EventHub**, you must obtain an **EventHub** instance through the member variable **context** of the **Ability** instance.
```js
```js
importAbilityfrom'@ohos.application.Ability'
importAbilityfrom'@ohos.application.Ability';
exportdefaultclassMainAbilityextendsAbility{
exportdefaultclassMainAbilityextendsAbility{
func1(){
func1(){
console.log("func1 is called");
console.log("func1 is called");
...
@@ -34,15 +34,15 @@ Subscribes to an event.
...
@@ -34,15 +34,15 @@ Subscribes to an event.
**Parameters**
**Parameters**
| Name| Type| Mandatory| Description|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| -------- | -------- | -------- | -------- |
| event | string | Yes| Event name.|
| event | string | Yes| Event name.|
| callback | Function | Yes| Callback invoked when the event is triggered.|
| callback | Function | Yes| Callback invoked when the event is triggered.|
**Example**
**Example**
```js
```js
importAbilityfrom'@ohos.application.Ability'
importAbilityfrom'@ohos.application.Ability';
exportdefaultclassMainAbilityextendsAbility{
exportdefaultclassMainAbilityextendsAbility{
onForeground(){
onForeground(){
...
@@ -72,15 +72,15 @@ Unsubscribes from an event. If **callback** is specified, this API unsubscribes
...
@@ -72,15 +72,15 @@ Unsubscribes from an event. If **callback** is specified, this API unsubscribes
**Parameters**
**Parameters**
| Name| Type| Mandatory| Description|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| -------- | -------- | -------- | -------- |
| event | string | Yes| Event name.|
| event | string | Yes| Event name.|
| callback | Function | No| Callback for the event. If **callback** is unspecified, all callbacks of the event are unsubscribed.|
| callback | Function | No| Callback for the event. If **callback** is unspecified, all callbacks of the event are unsubscribed.|
**Example**
**Example**
```js
```js
importAbilityfrom'@ohos.application.Ability'
importAbilityfrom'@ohos.application.Ability';
exportdefaultclassMainAbilityextendsAbility{
exportdefaultclassMainAbilityextendsAbility{
onForeground(){
onForeground(){
...
@@ -110,15 +110,15 @@ Triggers an event.
...
@@ -110,15 +110,15 @@ Triggers an event.
**Parameters**
**Parameters**
| Name| Type| Mandatory| Description|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| -------- | -------- | -------- | -------- |
| event | string | Yes| Event name.|
| event | string | Yes| Event name.|
| ...args | Object[] | Yes| Variable parameters, which are passed to the callback when the event is triggered.|
| ...args | Object[] | Yes| Variable parameters, which are passed to the callback when the event is triggered.|