• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# EventHub
2
3> **NOTE**
4>
5> 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.
6
7
8Implements event subscription, unsubscription, and triggering.
9
10## Modules to Import
11
12```js
13import Ability from '@ohos.application.Ability'
14```
15
16## Usage
17
18
19Before using any APIs in the **EventHub**, you must obtain an **EventHub** instance through the member variable **context** of the **Ability** instance.
20
21```js
22import Ability from '@ohos.application.Ability'
23export default class MainAbility extends Ability {
24    onForeground() {
25        this.context.eventHub.on("123", this.func1);
26    }
27}
28```
29
30
31## EventHub.on
32
33on(event: string, callback: Function): void;
34
35Subscribes to an event.
36
37**System capability**: SystemCapability.Ability.AbilityRuntime.Core
38
39**Parameters**
40
41| Name| Type| Mandatory| Description|
42| -------- | -------- | -------- | -------- |
43| event | string | Yes| Event name.|
44| callback | Function | Yes| Callback invoked when the event is triggered.|
45
46**Example**
47
48  ```js
49  import Ability from '@ohos.application.Ability'
50
51  export default class MainAbility extends Ability {
52      onForeground() {
53          this.context.eventHub.on("123", this.func1);
54          this.context.eventHub.on("123", () => {
55              console.log("call anonymous func 1");
56          });
57          // Result
58          // func1 is called
59          // call anonymous func 1
60          this.context.eventHub.emit("123");
61      }
62      func1() {
63          console.log("func1 is called");
64      }
65  }
66  ```
67
68
69## EventHub.off
70
71off(event: string, callback?: Function): void;
72
73Unsubscribes 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.
74
75**System capability**: SystemCapability.Ability.AbilityRuntime.Core
76
77**Parameters**
78
79| Name| Type| Mandatory| Description|
80| -------- | -------- | -------- | -------- |
81| event | string | Yes| Event name.|
82| callback | Function | No| Callback for the event. If **callback** is unspecified, all callbacks of the event are unsubscribed.|
83
84**Example**
85
86  ```js
87  import Ability from '@ohos.application.Ability'
88
89  export default class MainAbility extends Ability {
90      onForeground() {
91          this.context.eventHub.on("123", this.func1);
92          this.context.eventHub.off("123", this.func1); // Unsubscribe from func1.
93          this.context.eventHub.on("123", this.func1);
94          this.context.eventHub.on("123", this.func2);
95          this.context.eventHub.off("123"); // Unsubscribe from func1 and func2.
96      }
97      func1() {
98          console.log("func1 is called");
99      }
100      func2() {
101          console.log("func2 is called");
102      }
103  }
104  ```
105
106
107## EventHub.emit
108
109emit(event: string, ...args: Object[]): void;
110
111Triggers an event.
112
113**System capability**: SystemCapability.Ability.AbilityRuntime.Core
114
115**Parameters**
116
117| Name| Type| Mandatory| Description|
118| -------- | -------- | -------- | -------- |
119| event | string | Yes| Event name.|
120| ...args | Object[] | Yes| Variable parameters, which are passed to the callback when the event is triggered.|
121
122**Example**
123
124  ```js
125  import Ability from '@ohos.application.Ability'
126
127  export default class MainAbility extends Ability {
128      onForeground() {
129          this.context.eventHub.on("123", this.func1);
130          // Result
131          // func1 is called,undefined,undefined
132          this.context.eventHub.emit("123");
133          // Result
134          // func1 is called,1,undefined
135          this.context.eventHub.emit("123", 1);
136          // Result
137          // func1 is called,1,2
138          this.context.eventHub.emit("123", 1, 2);
139      }
140      func1(a, b) {
141          console.log("func1 is called," + a + "," + b);
142      }
143  }
144  ```
145