https://github.com/supercll/eventhub
a simple eventhub for vue
https://github.com/supercll/eventhub
Last synced: 3 months ago
JSON representation
a simple eventhub for vue
- Host: GitHub
- URL: https://github.com/supercll/eventhub
- Owner: supercll
- Created: 2020-10-02T08:40:39.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-03T14:17:06.000Z (almost 3 years ago)
- Last Synced: 2025-02-07T18:51:35.140Z (4 months ago)
- Language: TypeScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# EventHub
a simple eventhub for vue# 使用方法
见test.js文件# 存在问题:执行off产生数组塌陷
因为每次都是使用splice来切割数组的,所以off之后的事件直接消失,后面的事件就往前补空缺,使得下标index改变
```
const eventHub = new EventHub();
function fn1() {
console.log(1);
}
function fn2() {
console.log(2);
}
function fn3() {
console.log(3);
eventHub.off("init", fn1);
eventHub.off("init", fn2);
eventHub.off("init", fn3);
}
function fn4() {
console.log(4);
}
function fn5() {
console.log(5);
}
function fn6() {
console.log(6);
}
eventHub.on("init", fn1);
eventHub.on("init", fn2);
eventHub.on("init", fn3);
eventHub.on("init", fn4);
eventHub.on("init", fn5);
eventHub.on("init", fn6);
console.log("第一次执行事件");
eventHub.emit("init");
console.log("第二次执行事件");
eventHub.emit("init");
```
# 解决办法
如果有off取消事件,就先将off的事件指向null
在下一次emit触发事件的时候再重构事件数组