Skip to main content

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

NameType
EventsMapextends Record<string, any> = Event

Table of contents

Constructors

Methods

Constructors

constructor

new EventEmitter<EventsMap>(«destructured»?): EventEmitter<EventsMap>

Type parameters

NameType
EventsMapextends Record<string, any> = Event

Parameters

NameType
«destructured»IOptions

Returns

EventEmitter<EventsMap>

Defined in

src/core/Event.ts:49

Methods

on

on(type, handler, context?): EventEmitter<EventsMap>

添加订阅者

Parameters

NameTypeDescription
typeany事件类型
handlerany回调函数
context?any上下文

Returns

EventEmitter<EventsMap>

Defined in

src/core/Event.ts:82


once

once(type, handler, context?): EventEmitter<EventsMap>

添加一次性订阅者

Parameters

NameType
typeany
handlerany
context?any

Returns

EventEmitter<EventsMap>

Defined in

src/core/Event.ts:106


off

off(type, handler?, context?): EventEmitter<EventsMap>

取消监听

Parameters

NameType
typeany
handler?any
context?any

Returns

EventEmitter<EventsMap>

Defined in

src/core/Event.ts:130


emit

emit(type, args?): any

触发事件

Parameters

NameType
typeany
args?any

Returns

any

Defined in

src/core/Event.ts:158


has

has(type): any

Parameters

NameType
typeany

Returns

any

Defined in

src/core/Event.ts:167


clear

clear(): EventEmitter<EventsMap>

清空所有的订阅者

Returns

EventEmitter<EventsMap>

Defined in

src/core/Event.ts:174