vue: axios

加入Vue原型链:css

import Vue from "vue";
import QS from "qs";
import axios from "axios";
import App from "./app.vue";
import Element from "element-ui";
import router from "./configs/routes";
import './styles/_element.variables.scss';
import './styles/main.scss';

Vue.use(Element);
Vue.prototype.qs = QS;
Vue.prototype.$axios = axios;

new Vue({
    router,
    render: (h) => h(App)
}).$mount(document.getElementById('root'))

封装axios:vue

import axios from 'axios';

axios.defaults.timeout = 5000;
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

axios.interceptors.request.use(
    (config) => {
        if (config.data && config.data.$skipAuthHandler) {
            config.$skipAuthHandler = true;
            delete config.data.$skipAuthHandler;
        }
        if (config.params && config.params.$skipAuthHandler) {
            config.$skipAuthHandler = true;
            delete config.params.$skipAuthHandler;
        }
        //config.headers.Authorization = _loginUser.authorization();
        return config;
    },
    (error) => {
        return Promise.reject(error)
    }
);

axios.interceptors.response.use(
    (response) => {return Promise.resolve(response);
    },
    (error) => {
        const err = error.response;
        if (err.status === 401 && !!err.config && !err.config.$skipAuthHandler) {
            //_loginUser.clear();
            window.location = '/unauthorization';
        }
        toastr.error(err.data.message);
        return Promise.reject(error);
    }
);

function fetchPost(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err);
            })
    })
}

function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, { params: param })
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err);
            })
    })
}

function fetchRequest(param) {
    return new Promise((resolve, reject) => {
        axios.request({
            url: param.url || '',
            method: param.method || 'GET',
            data: param.datq || null,
            params: param.params || '',
            headers: {
                'Content-Type': param.contenType || "application/json;charset=UTF-8"
            }
        }).then(res => {
            typeof resolve === 'function' && resolve(res);
        }).catch(err => {
            typeof reject === 'function' && reject(err);
        })
    })
}
export default { fetchGet, fetchPost, fetchRequest }

引用ios

import https from '../https.js'   // 注意用本身的路径

            loginPost: function () {
                let params ={'username': 'admin', 'password': 'admin123', 'rememberMe': 'true','isMobile':'1'}
                https.fetchPost('/login',params ).then((data) => {
                    this.base.token = data.data.token    
                   
                    this.indexPost2(this.rres)
                }).catch(err=>{
                        console.log(err)
                    }
                )
            },
            indexPost2:function (date) {
                var this_ = this
                this_.check = false
                var jobj ={data:{'menuDate': date,'token':this.base.token}}
                let string  = JSON.stringify(jobj)
                let params = {dailyInfo:string}
                https.fetchPost('/meals/mobile/getDailyMenuByDate', params)
                .then((data) => {
                    this_.base.indexData = data
                    this_.check = true
                })
                .catch((err)=>{
                    console.log(err)

                })
            }
        },