class Bus {
constructor () {
this.subs = {}
}
$on (type, handler) {
// 判断是否是第一次注册
this.subs[type] = this.subs[type] || []
this.subs[type].push(handler)
}
$emit (type, ...rest) {
this.subs[type].forEach(typeItem => {
typeItem(...rest)
})
}
}
let bus = new Bus()
bus.$on('show', function (...rest) {
console.log(...rest)
})
bus.$on('show', function (...rest) {
console.log(...rest)
})
bus.$emit('show', '测试')
转载: 孤独患者yyg