1e41f4b71Sopenharmony_ci# EventHub 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **EventHub** module provides APIs to subscribe to, unsubscribe from, and trigger events. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - 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. 8e41f4b71Sopenharmony_ci> - The APIs of this module can be used only in the stage model. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Modules to Import 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Usage 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciBefore using any APIs in the **EventHub**, you must obtain an **EventHub** instance through the member variable **context** of the **UIAbility** instance. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci```ts 21e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 24e41f4b71Sopenharmony_ci eventFunc() { 25e41f4b71Sopenharmony_ci console.log('eventFunc is called'); 26e41f4b71Sopenharmony_ci } 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci onCreate() { 29e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', this.eventFunc); 30e41f4b71Sopenharmony_ci } 31e41f4b71Sopenharmony_ci} 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ciEventHub is not a global event center. Different context objects have different EventHub objects. Event subscription, unsubscription, and triggering are performed on a specific EventHub object. Therefore, EventHub cannot be used for event transmission between VMs or processes. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci## EventHub.on 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_cion(event: string, callback: Function): void; 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciSubscribes to an event. 40e41f4b71Sopenharmony_ci> **NOTE** 41e41f4b71Sopenharmony_ci> 42e41f4b71Sopenharmony_ci> When the callback is triggered by **emit**, the invoker is the **EventHub** object. To change the direction of **this** in **callback**, use an arrow function. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci**Parameters** 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 51e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 52e41f4b71Sopenharmony_ci| event | string | Yes | Event name. | 53e41f4b71Sopenharmony_ci| callback | Function | Yes | Callback invoked when the event is triggered. | 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**Error codes** 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| ID | Error Message | 60e41f4b71Sopenharmony_ci| ------- | -------- | 61e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Example 1** 64e41f4b71Sopenharmony_ciWhen the callback is triggered by **emit**, the invoker is the **EventHub** object. The **EventHub** object does not have the **value** property. Therefore, the result **undefined** is returned. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci```ts 67e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 70e41f4b71Sopenharmony_ci value: number = 12; 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci onCreate() { 73e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', this.eventFunc); 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci onForeground() { 77e41f4b71Sopenharmony_ci // Result 78e41f4b71Sopenharmony_ci // eventFunc is called, value: undefined 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci this.context.eventHub.emit('myEvent'); 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci eventFunc() { 84e41f4b71Sopenharmony_ci console.log(`eventFunc is called, value: ${this.value}`); 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci} 87e41f4b71Sopenharmony_ci``` 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**Example 2** 90e41f4b71Sopenharmony_ciWhen the callback uses an arrow function, the invoker is the **EntryAbility** object. The **EntryAbility** object has the **value** property. Therefore, the result **12** is returned. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci```ts 93e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 96e41f4b71Sopenharmony_ci value: number = 12; 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci onCreate() { 99e41f4b71Sopenharmony_ci // Anonymous functions can be used to subscribe to events. 100e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', () => { 101e41f4b71Sopenharmony_ci console.log(`anonymous eventFunc is called, value: ${this.value}`); 102e41f4b71Sopenharmony_ci }); 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci onForeground() { 106e41f4b71Sopenharmony_ci // Result 107e41f4b71Sopenharmony_ci // anonymous eventFunc is called, value: 12 108e41f4b71Sopenharmony_ci this.context.eventHub.emit('myEvent'); 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci eventFunc() { 112e41f4b71Sopenharmony_ci console.log(`eventFunc is called, value: ${this.value}`); 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci} 115e41f4b71Sopenharmony_ci``` 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci## EventHub.off 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_cioff(event: string, callback?: Function): void; 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ciUnsubscribes from an event. 122e41f4b71Sopenharmony_ci - If **callback** is specified, this API unsubscribes from the given event with the specified callback. 123e41f4b71Sopenharmony_ci - If **callback** is not specified, this API unsubscribes from the given event with all callbacks. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**Parameters** 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 132e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 133e41f4b71Sopenharmony_ci| event | string | Yes | Event name. | 134e41f4b71Sopenharmony_ci| callback | Function | No | Callback for the event. If **callback** is unspecified, the given event with all callbacks is unsubscribed. | 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**Error codes** 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci| ID | Error Message | 141e41f4b71Sopenharmony_ci| ------- | -------- | 142e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci**Example** 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci```ts 147e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 150e41f4b71Sopenharmony_ci onCreate() { 151e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', this.eventFunc1); 152e41f4b71Sopenharmony_ci this.context.eventHub.off('myEvent', this.eventFunc1); // Unsubscribe from the myEvent event with the callback eventFunc1. 153e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', this.eventFunc1); 154e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', this.eventFunc2); 155e41f4b71Sopenharmony_ci this.context.eventHub.off('myEvent'); // Unsubscribe from the myEvent event with all the callbacks (eventFunc1 and eventFunc2). 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci eventFunc1() { 159e41f4b71Sopenharmony_ci console.log('eventFunc1 is called'); 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci eventFunc2() { 163e41f4b71Sopenharmony_ci console.log('eventFunc2 is called'); 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci} 166e41f4b71Sopenharmony_ci``` 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci## EventHub.emit 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ciemit(event: string, ...args: Object[]): void; 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ciTriggers an event. 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci**Parameters** 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 181e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 182e41f4b71Sopenharmony_ci| event | string | Yes | Event name. | 183e41f4b71Sopenharmony_ci| ...args | Object[] | No | Variable parameters, which are passed to the callback when the event is triggered. | 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci**Error codes** 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci| ID | Error Message | 190e41f4b71Sopenharmony_ci| ------- | -------- | 191e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**Example** 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci```ts 196e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 199e41f4b71Sopenharmony_ci onCreate() { 200e41f4b71Sopenharmony_ci this.context.eventHub.on('myEvent', this.eventFunc); 201e41f4b71Sopenharmony_ci } 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci onDestroy() { 204e41f4b71Sopenharmony_ci // Result 205e41f4b71Sopenharmony_ci // eventFunc is called,undefined,undefined 206e41f4b71Sopenharmony_ci this.context.eventHub.emit('myEvent'); 207e41f4b71Sopenharmony_ci // Result 208e41f4b71Sopenharmony_ci // eventFunc is called,1,undefined 209e41f4b71Sopenharmony_ci this.context.eventHub.emit('myEvent', 1); 210e41f4b71Sopenharmony_ci // Result 211e41f4b71Sopenharmony_ci // eventFunc is called,1,2 212e41f4b71Sopenharmony_ci this.context.eventHub.emit('myEvent', 1, 2); 213e41f4b71Sopenharmony_ci } 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci eventFunc(argOne: number, argTwo: number) { 216e41f4b71Sopenharmony_ci console.log(`eventFunc is called, ${argOne}, ${argTwo}`); 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci} 219e41f4b71Sopenharmony_ci``` 220