使用Vue实例作为EventEmitter

参考链接

eventemitter.js

1
2
3
import Vue from 'vue'
export default new Vue()

main.js

1
2
3
4
import events from 'eventemitter'
//loadUrl调用的方法
window.onXxxx = (params) => events.$emit('xxxx',...params)

xxx.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import events from 'eventemitter'
export default {
data(){
return{
}
},
//组件创建时添加监听事件
created(){
events.$on('xxxx',() => {
//do something
})
}
//组件销毁时取消事件监听
destroyed(){
events.$off('xxxx')
}
}