1# EventHub 2 3EventHub模块提供了事件中心,提供订阅、取消订阅、触发事件的能力。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 本模块接口仅可在Stage模型下使用。 9 10## 导入模块 11 12```ts 13import common from '@ohos.app.ability.common'; 14``` 15 16## 使用说明 17 18在使用eventHub的功能前,需要通过UIAbility实例的成员变量context获取。 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 38订阅指定事件。 39 40**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 41 42**参数:** 43 44| 参数名 | 类型 | 必填 | 说明 | 45| -------- | -------- | -------- | -------- | 46| event | string | 是 | 事件名称。 | 47| callback | Function | 是 | 事件回调,事件触发后调用。 | 48 49**示例:** 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 // 支持使用匿名函数订阅事件 58 this.context.eventHub.on('myEvent', () => { 59 console.log('call anonymous eventFunc'); 60 }); 61 // 结果: 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 77取消订阅指定事件。 78 - 传入callback:取消指定的callback对指定事件的订阅,当该事件触发后,将不会回调该callback。 79 - 不传callback:取消所有callback对指定事件的订阅。 80 81**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 82 83**参数:** 84 85| 参数名 | 类型 | 必填 | 说明 | 86| -------- | -------- | -------- | -------- | 87| event | string | 是 | 事件名称。 | 88| callback | Function | 否 | 事件回调。如果不传callback,则取消订阅该事件下所有callback。 | 89 90**示例:** 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); // 取消eventFunc1对myEvent事件的订阅 99 this.context.eventHub.on('myEvent', this.eventFunc1); 100 this.context.eventHub.on('myEvent', this.eventFunc2); 101 this.context.eventHub.off('myEvent'); // 取消eventFunc1和eventFunc2对myEvent事件的订阅 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 118触发指定事件。 119 120**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 121 122**参数:** 123 124| 参数名 | 类型 | 必填 | 说明 | 125| -------- | -------- | -------- | -------- | 126| event | string | 是 | 事件名称。 | 127| ...args | Object[] | 否 | 可变参数,事件触发时,传递给回调函数的参数。 | 128 129**示例:** 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 // 结果: 138 // eventFunc is called,undefined,undefined 139 this.context.eventHub.emit('myEvent'); 140 // 结果: 141 // eventFunc is called,1,undefined 142 this.context.eventHub.emit('myEvent', 1); 143 // 结果: 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