大家好,这是我的EventBus公共文件
class EventBus {
constructor() {
this.events = this.events || new Object();
}
}
//首先构造函数需要存储event事件,使用键值对存储
//然后我们需要发布事件,参数是事件的type和需要传递的参数
EventBus.prototype.emit = function(type, ...args) {
let e;
e = this.events[type];
// 查看这个type的event有多少个回调函数,如果有多个需要依次调用。
if (Array.isArray(e)) {
for (let i = 0; i < e.length; i++) {
e[i].apply(this, args);
}
} else {
e[0].apply(this, args);
}
};
//然后我们需要写监听函数,参数是事件type和触发时需要执行的回调函数
EventBus.prototype.addListener = function(type, fun) {
const e = this.events[type];
if (!e) { //如果从未注册过监听函数,则将函数放入数组存入对应的键名下
this.events[type]= [fun];
} else { //如果注册过,则直接放入
e.push(fun);
}
};
const eventBus = new EventBus();
export default eventBus;
要使用的话在需要使用的界面导入
import EventBus from '@/utils/listent';
发送
EventBus.emit("test","testArray");
监听获取数据
EventBus.addListener('test',(testArray)=>{
console.log("得到的数据",testArray)
})
现在我想知道的是,如何改写公共的EventBus文件,使其监听获取到数据后能销毁,并且还能继续监听~~
帮我解决问题的话,可以私信我你V,我发红包感谢,这么多双眼睛看着不会骗人的,请大家监督!!
bus最好像定时那样主动清除,可以扩展一个 EventBus.clear 方法。通过bus_id清除。
有兴趣可以看看手写的原生 Bus
https://github.com/bestime/cr...