1//events - a super-basic Javascript (publish subscribe) pattern
2
3class Event{
4 constructor(){
5 this.events = {};
6 }
7
8 on(eventName, fn) {
9 this.events[eventName] = this.events[eventName] || [];
10 this.events[eventName].push(fn);
11 }
12
13 off(eventName, fn) {
14 if (this.events[eventName]) {
15 for (var i = 0; i < this.events[eventName].length; i++) {
16 if (this.events[eventName][i] === fn) {
17 this.events[eventName].splice(i, 1);
18 break;
19 }
20 };
21 }
22 }
23
24 trigger(eventName, data) {
25 if (this.events[eventName]) {
26 this.events[eventName].forEach(function(fn) {
27 fn(data);
28 });
29 }
30 }
31}
32
33export default new Event();
34
1import Vue from 'vue';
2import $bus from '.../event.js';
3
4const app = Vue.createApp({})
5app.config.globalProperties.$bus = $bus;
6