axios 请求二次封装

204 阅读1分钟

在src common api 下新建一个require.js文件 npm install axios

import axios from 'axios';
    import {
        Indicator
    } from 'mint-ui';
    export default {
        common: {
            method: 'GET',
            data: {},
            params: {}
        },
        $axios(ocptions = {}) {
            ocptions.method = ocptions.method || this.common.method;
            ocptions.data = ocptions.data || this.common.data;
            ocptions.params = ocptions.params || this.common.params

        // 请求获取数据前,开启loading状态
        Indicator.open('Loading...');

        return axios(ocptions).then(v => {
            let data = v.data.data
            return new Promise((res, rej) => {
                if (!v) return rej();
                // 关闭loading
                setTimeout(() => {
                    Indicator.close();
                }, 300);
                res(data)
            });

        })
    }
}