yoho-plugin-core.js
2.68 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* 插件
*/
import Router from 'vue-router';
import Promise from 'promise-polyfill';
import iView from 'iview';
import store from 'yoho-store';
import cookie from 'yoho-cookie';
import components from '../components/global';
import axios from 'axios';
import config from 'config';
import _ from 'lodash';
const plugin = {
loadGlobalComponents(Vue) {
_.each(components, component => {
Vue.component(component.name, component);
});
},
defineVueProp(Vue) {
Vue.prop = (key, value) => {
Vue[`$${key}`] = Vue.prototype[`$${key}`] = value;
};
Vue.beforeRenderHooks = [];
Vue.beforeRender = (fn) => {
Vue.beforeRenderHooks.push(fn);
};
Vue.render = opts => {
return new Promise(resolve => {
if (Vue.beforeRenderHooks.length) {
let step = index => {
if (index >= Vue.beforeRenderHooks.length) {
resolve();
} else {
Vue.beforeRenderHooks[index](() => {
step(index + 1);
});
}
};
step(0);
} else {
resolve();
}
}).then(() => {
return new Vue(Object.assign(opts, {
router: Vue.$router
}));
});
};
},
compatible() {
// 兼容IE的Function没有name属性为题,为了修复iView的bug
if (!(function f() {}).name) {
Object.defineProperty(Function.prototype, 'name', { //eslint-disable-line
get: function() {
let name = (this.toString().match(/^function\s*([^\s(]+)/) || [])[1];
Object.defineProperty(this, 'name', { value: name });
return name;
}
});
}
// 使用了webpack code spliting IE下需要promise ployfill
if (!window.Promise) {
window.Promise = Promise;
}
},
install(Vue) {
// 定义Vue全局属性
this.defineVueProp(Vue);
// 加载核心组件
this.loadGlobalComponents(Vue);
// 加载核心插件
Vue.use(iView);
Vue.use(Router);
// 附加Vue原型属性
Vue.prop('config', config);
Vue.prop('store', store);
Vue.prop('cookie', cookie);
// 设置axios默认参数
axios.defaults.baseURL = config.axiosBaseUrl;
axios.defaults.responseType = config.axiosResponseType;
}
};
export default plugin;