1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
| class EventPublic {
| constructor() {
| this.event = {}
| }
| $on(type, callback) {
| if (!this.event[type]) {
| this.event[type] = [callback]
| } else {
| this.event[type].push(callback)
| }
| }
| $emit(type, ...args) {
| if (!this.event[type]) {
| return
| }
| this.event[type].forEach(res => {
| res.apply(this, args)
| })
| }
| $off(type, callback) {
| if (!this.event[type]) {
| return
| }
| this.event[type] = this.event[type].filter(res => {
| return res != callback
| })
| }
| // 执行一次
| $once(type, callback) {
| function f() {
| callback()
| this.$off(type, f)
| }
| this.$on(type, f)
| }
| }
| export const EventBus = new EventPublic()
|
|
|