-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvueEventBus.js
More file actions
70 lines (64 loc) · 2.06 KB
/
vueEventBus.js
File metadata and controls
70 lines (64 loc) · 2.06 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @Author: zerojer994@gmail.com
* @Date: 2018-08-02 22:55:56
* @Last Modified by: zerojer994@gmail.com
* @Last Modified time: 2018-08-02 23:03:07
* @Description: eventEmitter for Vue, automatically cancel component event listeners
* @usage:
* import eventBus form 'xx/vueEventBus.js'
* Vue.use(eventBus)
* And then you can use this.$eventBusOn(event, func) to create event listeners
* Use this.$eventBusEmit(event, ...rest) to pass event, you can have all payload
* First of all you should write out all events will be mointored on the eventsRegistration option, which can help you manage your application events
*/
const eventBus = {
// Write out which events will be monitored
eventsRegistration: [
'test'
],
events: {},
on (event, func, vue) {
if (!this.eventsRegistration.includes(event)) {
throw new Error(`监听的${event}事件未注册,请在vueEventBus.js里注册`)
}
const sign = {func, vue}
if (this.events[event]) {
this.events[event].push(sign)
} else {
this.events[event] = [sign]
}
// Use the event hook to listen for the component's beforeDestroy event
// Clear the event response listened to by this component
vue.$once('hook:beforeDestroy', () => {
const index = this.events[event].findIndex(item => item.vue === vue)
this.events[event].splice(index, 1)
})
},
emit (event, ...rest) {
if (!this.events[event]) {
throw new Error(`${event}事件未被组件监听,请确定是否需要emit该事件`)
}
this.events[event].forEach(item => {
item.func(...rest)
})
},
init () {
this.eventsRegistration.forEach((item, index, arr) => {
if (arr.indexOf(item) !== index) {
console.error(`含有重复的${item}事件定义`)
}
})
}
}
const plugin = {
install (Vue) {
eventBus.init()
Vue.prototype.$eventBusOn = function (event, func) {
eventBus.on(event, func, this)
}
Vue.prototype.$eventBusEmit = function (event, ...rest) {
eventBus.emit(event, ...rest)
}
}
}
export default plugin