• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Emitter
2
3> 说明:本模块首批接口从API version 7开始支持。
4
5## 导入模块
6
7```javascript
8import emitter from '@ohos.events.emitter'
9```
10
11## 权限列表
12
1314
15## EventPriority
16
17用于表示事件被投递的优先级。
18
19| 名称      | 值   | 说明                                              |
20| --------- | ---- | ------------------------------------------------- |
21| IMMEDIATE | 0    | 表示事件被立即投递。<br/>**系统能力**: SystemCapability.Notification.Emitter                                |
22| HIGH      | 1    | 表示事件先于LOW优先级投递。<br/>**系统能力**: SystemCapability.Notification.Emitter                         |
23| LOW       | 2    | 表示事件优于IDLE优先级投递,事件的默认优先级是LOW。<br/>**系统能力**: SystemCapability.Notification.Emitter |
24| IDLE      | 3    | 表示在没有其他事件的情况下,才投递该事件。<br/>**系统能力**: SystemCapability.Notification.Emitter          |
25
26## emitter.on
27
28on(event: [InnerEvent](#innerevent), callback: Callback\<[EventData](#eventdata)\>): void
29
30持续订阅某个事件以及接收事件的回调处理。
31
32**系统能力**: SystemCapability.Notification.Emitter
33
34**参数:**
35
36| 参数名   | 类型                                | 必填 | 说明                     |
37| -------- | ----------------------------------- | ---- | ------------------------ |
38| event    | [InnerEvent](#innerevent)           | 是   | 持续订阅的事件           |
39| callback | Callback\<[EventData](#eventdata)\> | 是   | 接收订阅事件时的回调处理 |
40
41**示例:**
42
43```javascript
44var innerEvent = {
45    eventId: 1
46};
47var callback = (eventData) => {
48    console.info('callback');
49};
50emitter.on(innerEvent, callback);
51```
52
53## emitter.once
54
55once(event: [InnerEvent](#innerevent), callback: Callback\<[EventData](#eventdata)\>): void
56
57单次订阅某个事件以及接收事件的回调处理,接收到回调处理后自动取消订阅。
58
59**系统能力**: SystemCapability.Notification.Emitter
60
61**参数:**
62
63| 参数名   | 类型                                | 必填 | 说明                     |
64| -------- | ----------------------------------- | ---- | ------------------------ |
65| event    | [InnerEvent](#innerevent)           | 是   | 单次订阅的事件           |
66| callback | Callback\<[EventData](#eventdata)\> | 是   | 接收订阅事件时的回调处理 |
67
68**示例:**
69
70```javascript
71var innerEvent = {
72    eventId: 1
73};
74var callback = (eventData) => {
75    console.info('once callback');
76};
77emitter.once(innerEvent, callback);
78```
79
80## emitter.off
81
82off(eventId: number): void
83
84取消订阅某个事件。
85
86**系统能力**: SystemCapability.Notification.Emitter
87
88**参数:**
89
90| 参数名  | 类型   | 必填 | 说明   |
91| ------- | ------ | ---- | ------ |
92| eventId | number | 是   | 事件ID |
93
94**示例:**
95
96```javascript
97emitter.off(1);
98```
99
100## emitter.emit
101
102emit(event: InnerEvent, data?: EventData): void
103
104发送一个事件到事件队列。
105
106**系统能力**: SystemCapability.Notification.Emitter
107
108**参数:**
109
110| 参数名 | 类型                      | 必填 | 说明           |
111| ------ | ------------------------- | ---- | -------------- |
112| event  | [InnerEvent](#innerevent) | 是   | 发送的事件     |
113| data   | [EventData](#eventdata)   | 否   | 事件携带的数据 |
114
115**示例:**
116
117```javascript
118var eventData = {
119    data: {
120        "content": "c",
121        "id": 1,
122    }};
123var innerEvent = {
124    eventId: 1,
125    priority: emitter.EventPriority.HIGH
126};
127emitter.emit(innerEvent, eventData);
128```
129
130## InnerEvent
131
132进程内的事件。
133
134| 名称     | 参数类型                        | 可读 | 可写 | 说明                               |
135| -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
136| eventId  | number                          | 是   | 是   | 事件的ID,由开发者定义用来辨别事件。<br/>**系统能力**: SystemCapability.Notification.Emitter |
137| priority | [EventPriority](#eventpriority) | 是   | 是   | 事件被投递的优先级。<br/>**系统能力**: SystemCapability.Notification.Emitter                 |
138
139## EventData
140
141发送事件时传递的数据。
142
143| 名称 | 参数类型           | 可读 | 可写 | 说明           |
144| ---- | ------------------ | ---- | ---- | -------------- |
145| data | [key: string]: any | 是   | 是   | 发送事件时传递的数据,数据类型支持字符串、整型和布尔型。<br/>**系统能力**: SystemCapability.Notification.Emitter |
146