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