1.0.1 • Published 2 years ago

event-dispatch-js v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Describe

自定义事件调度器提供了通过调度事件和侦听事件在应用程序内部进行通信的能力。

Installation

$ npm i event-dispatch-js

API

  • on(eventName, callback) 添加一组事件侦听器
  • emit(eventName, detail) 触发一组事件侦听器
  • off(eventName, callback) 移除一组事件侦听器

使用默认导入实现全局事件侦听与触发

// a.js
import eventBus from 'event-dispatch-js';

function eventListener(detail) {
	console.log('事件回调参数:', detail);
}
// 注册一组侦听器
eventBus.on('custom-event-name', eventListener);
// 移除一组侦听器,当侦听回调函数参数为空时,将会清空当前事件下所有的侦听器
eventBus.off('custom-event-name', eventListener);

// b.js
import eventBus from 'event-bus-js';
// 触发一组事件
eventBus.emit('custom-event-name', {
	type: 'custom-event-name'
});

局部独立使用

import { EventBus } from 'event-dispatch-js';

// 实例化事件总线
const eventBus = new EventBus();

License

MIT