yoho-plugin-core.js
3.45 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* 插件
*/
import Router from 'vue-router';
import Promise from 'promise-polyfill';
import iView from 'iview';
import 'iview/dist/styles/iview.css';
import 'iview/dist/styles/fonts/ionicons.eot';
import 'iview/dist/styles/fonts/ionicons.svg';
import 'iview/dist/styles/fonts/ionicons.ttf';
import 'iview/dist/styles/fonts/ionicons.woff';
import '../fonts/iconfont.css';
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';
export default {
loadGlobalComponents(Vue) {
_.each(components, component => {
if (component.length) {
Vue.component(component[0], component[1]);
} else {
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);
// 兼容
this.compatible();
// 加载核心插件
Vue.use(iView);
Vue.use(Router);
// 附加Vue原型属性
Vue.prop('config', config[process.env.NODE_ENV]);
Vue.prop('store', store);
Vue.prop('cookie', cookie);
// 设置axios默认参数
axios.defaults.baseURL = Vue.$config.axiosBaseUrl;
axios.defaults.responseType = Vue.$config.axiosResponseType;
axios.interceptors.response.use(response => {
if (response.status >= 200 && response.status < 300) {
return response;
}
iView.Message.error('接口异常');
return Promise.reject({response});
}, error => {
return Promise.reject(error);
});
}
};