Skip to content

Class: EventBus

Example

ts
// 总线
let count = 0;
EventBus.on('test', function (num, num1) {
  count = num + num1;
})
EventBus.emit('test', 1, 2);
expect(count).toBe(3);

// 分线
let count = 0;
const bus = new EventBus();
bus.on('test', function (num, num1) {
  count = num + num1;
})
bus.emit('test', 3, 4);
expect(count).toBe(7);

Constructors

new EventBus()

ts
new EventBus(config?: EventBusConfig): EventBus

Parameters

ParameterType

config?

EventBusConfig

Returns

EventBus

Properties

PropertyModifierType
emitpublic(key: string, ...rest: any[]) => void
onpublic(key: string, func: (...rest: any[]) => void) => void

Methods

emit()

ts
static emit(key: string, ...rest: any[]): void

触发事件

Parameters

ParameterTypeDescription

key

string

事件名

...rest

any[]

传给回调函数的参数

Returns

void


on()

ts
static on(key: string, func: (...rest: any[]) => void): void

监听事件

Parameters

ParameterTypeDescription

key

string

事件名

func

(...rest: any[]) => void

回调函数

Returns

void