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
15
import UIAbility from '@ohos.app.ability.UIAbility';
H
HuangXW 已提交
16

17
export default class EntryAbility extends UIAbility {
H
HuangXW 已提交
18
    eventFunc(){
M
mingxihua 已提交
19
        console.log('eventFunc is called');
Y
yuyaozhi 已提交
20
    }
H
HuangXW 已提交
21

ahjxliubao2's avatar
ahjxliubao2 已提交
22
    onForeground() {
M
mingxihua 已提交
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
```ts
46
import UIAbility from '@ohos.app.ability.UIAbility';
H
HuangXW 已提交
47

48
export default class EntryAbility extends UIAbility {
H
HuangXW 已提交
49
    onForeground() {
M
mingxihua 已提交
50
        this.context.eventHub.on('myEvent', this.eventFunc);
H
HuangXW 已提交
51
        // 支持使用匿名函数订阅事件
M
mingxihua 已提交
52 53
        this.context.eventHub.on('myEvent', () => {
            console.log('call anonymous eventFunc');
H
HuangXW 已提交
54 55 56 57
        });
        // 结果:
        // eventFunc is called
        // call anonymous eventFunc
M
mingxihua 已提交
58
        this.context.eventHub.emit('myEvent'); 
H
HuangXW 已提交
59 60 61
    }

    eventFunc() {
M
mingxihua 已提交
62
        console.log('eventFunc is called');
H
HuangXW 已提交
63 64 65
    }
}
```
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
```ts
87
import UIAbility from '@ohos.app.ability.UIAbility';
H
HuangXW 已提交
88

89
export default class EntryAbility extends UIAbility {
H
HuangXW 已提交
90
    onForeground() {
M
mingxihua 已提交
91 92 93 94 95
        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事件的订阅
H
HuangXW 已提交
96 97 98
    }

    eventFunc1() {
M
mingxihua 已提交
99
        console.log('eventFunc1 is called');
H
HuangXW 已提交
100 101 102
    }

    eventFunc2() {
M
mingxihua 已提交
103
        console.log('eventFunc2 is called');
H
HuangXW 已提交
104 105 106
    }
}
```
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

```ts
126
import UIAbility from '@ohos.app.ability.UIAbility';
H
HuangXW 已提交
127

128
export default class EntryAbility extends UIAbility {
H
HuangXW 已提交
129
    onForeground() {
M
mingxihua 已提交
130
        this.context.eventHub.on('myEvent', this.eventFunc);
H
HuangXW 已提交
131 132
        // 结果:
        // eventFunc is called,undefined,undefined
M
mingxihua 已提交
133
        this.context.eventHub.emit('myEvent');
H
HuangXW 已提交
134 135
        // 结果:
        // eventFunc is called,1,undefined
M
mingxihua 已提交
136
        this.context.eventHub.emit('myEvent', 1);
H
HuangXW 已提交
137 138
        // 结果:
        // eventFunc is called,1,2
M
mingxihua 已提交
139
        this.context.eventHub.emit('myEvent', 1, 2);
H
HuangXW 已提交
140 141 142
    }

    eventFunc(argOne, argTwo) {
M
mingxihua 已提交
143
        console.log('eventFunc is called,' + argOne + ',' + argTwo);
H
HuangXW 已提交
144 145 146
    }
}
```