1 | /**
|
2 | * @author SoldierAb
|
3 | * @description Vue跨组件通信库
|
4 | * @example
|
5 | * import Bus from '@/utils/vueBus';
|
6 | * Vue.use(Bus);
|
7 | *
|
8 | * this.$bus.emit('refresh', ...args);
|
9 | *
|
10 | * created() {
|
11 | * this.$bus.on("refresh", fn);
|
12 | * },
|
13 | *
|
14 | * beforeDestroy() { // ~ attention ~ 一定要记得解绑事件,不然会造成内存泄漏哦
|
15 | * this.$bus.off("refresh");
|
16 | * },
|
17 | *
|
18 | */
|
19 |
|
20 | const install = (Vue)=>{
|
21 | const Bus = new Vue({
|
22 | methods:{
|
23 | emit(event,...args){
|
24 | this.$emit(event,...args);
|
25 | },
|
26 | on(event,callback){
|
27 | this.$on(event,callback);
|
28 | },
|
29 | off(event,callback){
|
30 | this.$off(event,callback);
|
31 | }
|
32 | }
|
33 | })
|
34 |
|
35 | Vue.prototype.$bus=Bus
|
36 | }
|
37 |
|
38 |
|
39 | export default install; |
\ | No newline at end of file |