vue.js 拦截器

import axios from 'axios'
import {Message, LoadingBar} from 'iview'
import {router} from '@/router/index'
import {apiUrl} from './env'

// 创建axios实例
const service = axios.create({
    baseURL: apiUrl,
    timeout: 60000, //请求超时时间一分钟
    withCredentials: true
});
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// request拦截器
service.interceptors.response.use(
    response => {

        if (response.data.retMsg) {
            if (response.data.retCode === "out") {
                router.push({
                    name: 'login'
                });
            }

            // 全局显示ajax请求信息
            // retCode === "success"
            if (response.data.retCode !== 'success') {
                Message.error(response.data.retMsg)
            } else {
                Message.info(response.data.retMsg)
            }
        }
        LoadingBar.finish();
        return response
    })
export default service

你可能感兴趣的:(vue.js 拦截器)