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