js-apis-inner-application-eventHub.md 4.0 KB
Newer Older
ahjxliubao2's avatar
ahjxliubao2 已提交
1 2
# EventHub

Y
yuyaozhi 已提交
3 4
EventHub模块提供了事件中心,提供订阅、取消订阅、触发事件的能力。

Y
yuyaozhi 已提交
5
> **说明:**
H
HuangXW 已提交
6 7 8
>
>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。  
>  - 本模块接口仅可在Stage模型下使用。
ahjxliubao2's avatar
ahjxliubao2 已提交
9 10 11

## 使用说明

H
HuangXW 已提交
12
在使用eventHub的功能前,需要通过UIAbility实例的成员变量context获取。
ahjxliubao2's avatar
ahjxliubao2 已提交
13

M
m00512953 已提交
14
```ts
H
HuangXW 已提交
15 16 17 18 19
import UIAbility from '@ohos.app.ability.UIAbility'

export default class MainAbility extends UIAbility {
    eventFunc(){
        console.log("eventFunc is called");
Y
yuyaozhi 已提交
20
    }
H
HuangXW 已提交
21

ahjxliubao2's avatar
ahjxliubao2 已提交
22
    onForeground() {
H
HuangXW 已提交
23
        this.context.eventHub.on("myEvent", this.eventFunc);
ahjxliubao2's avatar
ahjxliubao2 已提交
24 25 26 27
    }
}
```

Y
yuyaozhi 已提交
28
## EventHub.on
ahjxliubao2's avatar
ahjxliubao2 已提交
29 30 31 32 33

on(event: string, callback: Function): void;

订阅指定事件。

Y
yuyaozhi 已提交
34
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
35

Y
yuyaozhi 已提交
36
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
37

F
fangJinliang1 已提交
38 39 40
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| event | string | 是 | 事件名称。 |
H
HuangXW 已提交
41
| callback | Function | 是 | 事件回调,事件触发后调用。 |
ahjxliubao2's avatar
ahjxliubao2 已提交
42

Y
yuyaozhi 已提交
43
**示例:**
ahjxliubao2's avatar
ahjxliubao2 已提交
44

H
HuangXW 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
```ts
import UIAbility from '@ohos.app.ability.UIAbility'

export default class MainAbility extends UIAbility {
    onForeground() {
        this.context.eventHub.on("myEvent", this.eventFunc);
        // 支持使用匿名函数订阅事件
        this.context.eventHub.on("myEvent", () => {
            console.log("call anonymous eventFunc");
        });
        // 结果:
        // eventFunc is called
        // call anonymous eventFunc
        this.context.eventHub.emit("myEvent"); 
    }

    eventFunc() {
        console.log("eventFunc is called");
    }
}
```
ahjxliubao2's avatar
ahjxliubao2 已提交
66

Y
yuyaozhi 已提交
67
## EventHub.off
ahjxliubao2's avatar
ahjxliubao2 已提交
68 69 70

off(event: string, callback?: Function): void;

H
HuangXW 已提交
71 72 73
取消订阅指定事件。
 - 传入callback:取消指定的callback对指定事件的订阅,当该事件触发后,将不会回调该callback。
 - 不传callback:取消所有callback对指定事件的订阅。
ahjxliubao2's avatar
ahjxliubao2 已提交
74

Y
yuyaozhi 已提交
75
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
76

Y
yuyaozhi 已提交
77
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
78

F
fangJinliang1 已提交
79 80 81 82
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| event | string | 是 | 事件名称。 |
| callback | Function | 否 | 事件回调。如果不传callback,则取消订阅该事件下所有callback。 |
ahjxliubao2's avatar
ahjxliubao2 已提交
83

Y
yuyaozhi 已提交
84
**示例:**
ahjxliubao2's avatar
ahjxliubao2 已提交
85

H
HuangXW 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
```ts
import UIAbility from '@ohos.app.ability.UIAbility'

export default class MainAbility extends UIAbility {
    onForeground() {
        this.context.eventHub.on("myEvent", this.eventFunc1);
        this.context.eventHub.off("myEvent", this.eventFunc1); // 取消eventFunc1对myEvent事件的订阅
        this.context.eventHub.on("myEvent", this.eventFunc1);
        this.context.eventHub.on("myEvent", this.eventFunc2);
        this.context.eventHub.off("myEvent");  // 取消eventFunc1和eventFunc2对myEvent事件的订阅
    }

    eventFunc1() {
        console.log("eventFunc1 is called");
    }

    eventFunc2() {
        console.log("eventFunc2 is called");
    }
}
```
ahjxliubao2's avatar
ahjxliubao2 已提交
107

Y
yuyaozhi 已提交
108
## EventHub.emit
ahjxliubao2's avatar
ahjxliubao2 已提交
109 110 111 112 113

emit(event: string, ...args: Object[]): void;

触发指定事件。

Y
yuyaozhi 已提交
114
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
115

Y
yuyaozhi 已提交
116
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
117

F
fangJinliang1 已提交
118 119 120 121
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| event | string | 是 | 事件名称。 |
| ...args | Object[] | 是 | 可变参数,事件触发时,传递给回调函数的参数。 |
ahjxliubao2's avatar
ahjxliubao2 已提交
122

Y
yuyaozhi 已提交
123
**示例:**
H
HuangXW 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

```ts
import UIAbility from '@ohos.app.ability.UIAbility'

export default class MainAbility extends UIAbility {
    onForeground() {
        this.context.eventHub.on("myEvent", this.eventFunc);
        // 结果:
        // eventFunc is called,undefined,undefined
        this.context.eventHub.emit("myEvent");
        // 结果:
        // eventFunc is called,1,undefined
        this.context.eventHub.emit("myEvent", 1);
        // 结果:
        // eventFunc is called,1,2
        this.context.eventHub.emit("myEvent", 1, 2);
    }

    eventFunc(argOne, argTwo) {
        console.log("eventFunc is called," + argOne + "," + argTwo);
    }
}
```