1.0.1 • Published 17 days ago

listen-events v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
17 days ago

EventBus

EventBus 是一个简单的 WEB 端事件侦听器,提供了一种将事件分派给订阅的事件侦听器的机制。

安装

npm i listen-events

# or 

yarn add listen-events

案例

订阅事件

    import EventBus from "listen-events";

    const testEvent = (...args: any) => {
        console.log(args)
    }
    EventBus.on('test-event', testEvent)

分派事件

    import EventBus from "listen-events";

    const name = 'zhangsan', age = 18
    EventBus.emit("test-event", 1, {name, age})

    // 或者

    EventBus.emitAll("test-event", 1, {name, age})

关闭事件

    import EventBus from "listen-events";

    EventBus.offs('test-event', "test-event-2")

    // 或者

    const testEvent = (...args: any) => {
        console.log(args)
    }
    EventBus.off('test-event', testEvent)