Files
intc-vue3/src/utils/eventHub.js
2024-04-19 09:36:13 +08:00

35 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 创建公众号对象
const eventHub = {
// 创建缓存列表
list: {
// click:[f1,f2]
},
// 订阅
on: (name, fn) => {
// 如果对象中没有对应的 event 值,也就是说明没有订阅过,就给 event 创建个缓存列表
// 如有对象中有相应的 event 值,把 fn 添加到对应 event 的缓存列表里
eventHub.list[name] = eventHub.list[name] || []
eventHub.list[name].push(fn)
},
// 发布
emit: (name, data) => {
const q = eventHub.list[name]
//如果缓存列表里面为空,就返回
if (!q) return
//遍历当前event里的缓存列表并且依次执行
// eslint-disable-next-line no-useless-call
q.map((f) => f.call(null, data))
return undefined
},
off: (name, fn) => {
const q = eventHub.list[name]
//如果缓存列表里面为空,就返回
if (!q) return
//如果有,找到当前缓存列表里面对应的标号,并删除
const index = q.indexOf(fn)
if (index < 0) return
q.splice(index, 1)
}
}
export default eventHub