js-apis-eventhub.md 3.8 KB
Newer Older
1 2
# EventHub

W
wusongqing 已提交
3 4
> **NOTE**
>
5 6 7 8 9
> The initial APIs of this module are supported since API 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.


Implements event subscription, unsubscription, and triggering.

W
wusongqing 已提交
10 11 12 13 14
## Modules to Import

```js
import Ability from '@ohos.application.Ability'
```
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

## Usage


Before using any APIs in the **EventHub**, you must obtain an **EventHub** instance through the member variable **context** of the **Ability** instance.

```js
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
    onForeground() {
        this.context.eventHub.on("123", this.func1);
    }
}
```


## EventHub.on

on(event: string, callback: Function): void;

Subscribes to an event.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

W
wusongqing 已提交
41 42 43 44
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | string | Yes| Event name.|
| callback | Function | Yes| Callback invoked when the event is triggered.|
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

**Example**
    
  ```js
  import Ability from '@ohos.application.Ability'
  
  export default class MainAbility extends Ability {
      onForeground() {
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.on("123", () => {
              console.log("call anonymous func 1");
          });
          // Result
          // func1 is called
          // call anonymous func 1
          this.context.eventHub.emit("123"); 
      }
      func1() {
          console.log("func1 is called");
      }
  }
  ```


## EventHub.off

off(event: string, callback?: Function): void;

Unsubscribes from an event. If **callback** is specified, this API unsubscribes from the specified callback. If **callback** is not specified, this API unsubscribes from all callbacks in the event.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

W
wusongqing 已提交
79 80 81 82
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | string | Yes| Event name.|
| callback | Function | No| Callback for the event. If **callback** is unspecified, all callbacks of the event are unsubscribed.|
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

**Example**
    
  ```js
  import Ability from '@ohos.application.Ability'
  
  export default class MainAbility extends Ability {
      onForeground() {
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.off("123", this.func1); // Unsubscribe from func1.
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.on("123", this.func2);
          this.context.eventHub.off("123"); // Unsubscribe from func1 and func2.
      }
      func1() {
          console.log("func1 is called");
      }
      func2() {
          console.log("func2 is called");
      }
  }
  ```


## EventHub.emit

emit(event: string, ...args: Object[]): void;

Triggers an event.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

W
wusongqing 已提交
117 118 119 120
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | string | Yes| Event name.|
| ...args | Object[] | Yes| Variable parameters, which are passed to the callback when the event is triggered.|
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

**Example**
    
  ```js
  import Ability from '@ohos.application.Ability'
  
  export default class MainAbility extends Ability {
      onForeground() {
          this.context.eventHub.on("123", this.func1);
          // Result
          // func1 is called,undefined,undefined
          this.context.eventHub.emit("123");
          // Result
          // func1 is called,1,undefined
          this.context.eventHub.emit("123", 1);
          // Result
          // func1 is called,1,2
          this.context.eventHub.emit("123", 1, 2);
      }
      func1(a, b) {
          console.log("func1 is called," + a + "," + b);
      }
  }
  ```