您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

tiny-emitter.js:一个小型的事件订阅发布库

彭世瑜 发布时间:2022-07-24 16:55:21 ,浏览量:3

A tiny (less than 1k) event emitter library

文档

  • github: https://github.com/scottcorgan/tiny-emitter

安装

npm install tiny-emitter --save

使用示例


import Emitter from 'tiny-emitter'

const emitter = new Emitter()

function handler(data) {
    console.log(data);
}

const TOPIC = 'topic'

// 订阅
emitter.on(TOPIC, handler)   

// 仅订阅一次
emitter.once(TOPIC, handler)

// 发布事件1
emitter.emit(TOPIC, { a: '1' })

// 取消订阅
emitter.off(TOPIC, handler) 

// 发布事件2
emitter.emit(TOPIC, { a: '2' })
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1085s