Class: EventEmitter<EventsMap>
事件中心(实现的发布订阅者模式)
简单使用:
const e = new EventEmitter();
const handler = (data) => {
console.log(data);
};
e.on('eventName', handler);
e.emit('eventName', { data: 'data' });
// 取消订阅
e.off('eventName', handler);
// 清除所有的订阅者
e.clear();
Type parameters
Name | Type |
---|---|
EventsMap | extends Record <string , any > = Event |
Table of contents
Constructors
Methods
Constructors
constructor
• new EventEmitter<EventsMap
>(«destructured»?
): EventEmitter
<EventsMap
>
Type parameters
Name | Type |
---|---|
EventsMap | extends Record <string , any > = Event |
Parameters
Name | Type |
---|---|
«destructured» | IOptions |
Returns
EventEmitter
<EventsMap
>
Defined in
Methods
on
▸ on(type
, handler
, context?
): EventEmitter
<EventsMap
>
添加订阅者
Parameters
Name | Type | Description |
---|---|---|
type | any | 事件类型 |
handler | any | 回调函数 |
context? | any | 上下文 |
Returns
EventEmitter
<EventsMap
>
Defined in
once
▸ once(type
, handler
, context?
): EventEmitter
<EventsMap
>
添加一次性订阅者
Parameters
Name | Type |
---|---|
type | any |
handler | any |
context? | any |
Returns
EventEmitter
<EventsMap
>
Defined in
off
▸ off(type
, handler?
, context?
): EventEmitter
<EventsMap
>
取消监听
Parameters
Name | Type |
---|---|
type | any |
handler? | any |
context? | any |
Returns
EventEmitter
<EventsMap
>
Defined in
emit
▸ emit(type
, args?
): any
触发事件
Parameters
Name | Type |
---|---|
type | any |
args? | any |
Returns
any
Defined in
has
▸ has(type
): any
Parameters
Name | Type |
---|---|
type | any |
Returns
any
Defined in
clear
▸ clear(): EventEmitter
<EventsMap
>
清空所有的订阅者
Returns
EventEmitter
<EventsMap
>