• 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 **UIAbility** instance.
19
20```ts
21import UIAbility from '@ohos.app.ability.UIAbility';
22
23export default class EntryAbility extends UIAbility {
24    eventFunc(){
25        console.log('eventFunc is called');
26    }
27
28    onForeground() {
29        this.context.eventHub.on('myEvent', this.eventFunc);
30    }
31}
32```
33
34## EventHub.on
35
36on(event: string, callback: Function): void;
37
38Subscribes to an event.
39
40**System capability**: SystemCapability.Ability.AbilityRuntime.Core
41
42**Parameters**
43
44| Name| Type| Mandatory| Description|
45| -------- | -------- | -------- | -------- |
46| event | string | Yes| Event name.|
47| callback | Function | Yes| Callback invoked when the event is triggered.|
48
49**Example**
50
51```ts
52import UIAbility from '@ohos.app.ability.UIAbility';
53
54export default class EntryAbility extends UIAbility {
55    onForeground() {
56        this.context.eventHub.on('myEvent', this.eventFunc);
57        // Anonymous functions can be used to subscribe to events.
58        this.context.eventHub.on('myEvent', () => {
59            console.log('call anonymous eventFunc');
60        });
61        // Result
62        // eventFunc is called
63        // call anonymous eventFunc
64        this.context.eventHub.emit('myEvent');
65    }
66
67    eventFunc() {
68        console.log('eventFunc is called');
69    }
70}
71```
72
73## EventHub.off
74
75off(event: string, callback?: Function): void;
76
77Unsubscribes from an event.
78 - If **callback** is specified, this API unsubscribes from the given event with the specified callback.
79 - If **callback** is not specified, this API unsubscribes from the given event with all callbacks.
80
81**System capability**: SystemCapability.Ability.AbilityRuntime.Core
82
83**Parameters**
84
85| Name| Type| Mandatory| Description|
86| -------- | -------- | -------- | -------- |
87| event | string | Yes| Event name.|
88| callback | Function | No| Callback for the event. If **callback** is unspecified, the given event with all callbacks is unsubscribed.|
89
90**Example**
91
92```ts
93import UIAbility from '@ohos.app.ability.UIAbility';
94
95export default class EntryAbility extends UIAbility {
96    onForeground() {
97        this.context.eventHub.on('myEvent', this.eventFunc1);
98        this.context.eventHub.off('myEvent', this.eventFunc1); // Unsubscribe from the myEvent event with the callback eventFunc1.
99        this.context.eventHub.on('myEvent', this.eventFunc1);
100        this.context.eventHub.on('myEvent', this.eventFunc2);
101        this.context.eventHub.off('myEvent');  // Unsubscribe from the myEvent event with all the callbacks (eventFunc1 and eventFunc2).
102    }
103
104    eventFunc1() {
105        console.log('eventFunc1 is called');
106    }
107
108    eventFunc2() {
109        console.log('eventFunc2 is called');
110    }
111}
112```
113
114## EventHub.emit
115
116emit(event: string, ...args: Object[]): void;
117
118Triggers an event.
119
120**System capability**: SystemCapability.Ability.AbilityRuntime.Core
121
122**Parameters**
123
124| Name| Type| Mandatory| Description|
125| -------- | -------- | -------- | -------- |
126| event | string | Yes| Event name.|
127| ...args | Object[] | No| Variable parameters, which are passed to the callback when the event is triggered.|
128
129**Example**
130
131```ts
132import UIAbility from '@ohos.app.ability.UIAbility';
133
134export default class EntryAbility extends UIAbility {
135    onForeground() {
136        this.context.eventHub.on('myEvent', this.eventFunc);
137        // Result
138        // eventFunc is called,undefined,undefined
139        this.context.eventHub.emit('myEvent');
140        // Result
141        // eventFunc is called,1,undefined
142        this.context.eventHub.emit('myEvent', 1);
143        // Result
144        // eventFunc is called,1,2
145        this.context.eventHub.emit('myEvent', 1, 2);
146    }
147
148    eventFunc(argOne: number, argTwo: number) {
149        console.log(`eventFunc is called, ${argOne}, ${argTwo}`);
150    }
151}
152```
153