yoho-plugin-core.js 1.75 KB
/**
 * 插件
 */
import iView from 'iview';
import Router from 'vue-router';
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 = {
    install(Vue, options) {
        // 加载核心插件
        Vue.use(iView);
        Vue.use(Router);


        Vue.prop = (key, value) => {
            Vue[`$${key}`] = Vue.prototype[`$${key}`] = value;
        };

        // 附加Vue原型属性
        if (config) {
            Vue.prop('config', config);
        }
        if (options && options.router) {
            Vue.$router = options.router;
        }
        Vue.prop('store', store);
        Vue.prop('cookie', cookie);


        // 加载核心组件
        _.each(components, component => {
            Vue.component(component.name, component);
        });

        Vue.beforeRender = [];
        Vue.render = opts => {
            return new Promise(resolve => {
                if (Vue.beforeRender.length) {
                    let step = index => {
                        if (index >= Vue.beforeRender.length) {
                            resolve();
                        } else {
                            Vue.beforeRender[index](() => {
                                step(index + 1);
                            });
                        }
                    };

                    step(0);
                } else {
                    resolve();
                }
            }).then(() => {
                return new Vue(opts);
            });
        };

        // 设置axios默认参数
        axios.defaults.baseURL = '/Api';
        axios.defaults.responseType = 'json';
    }
};

export default plugin;