vue学习---vue项目中的axios请求与相应拦截

import axios from "axios";
import store from "../store";
import router from "../router";

export default function setAxios() {
    //请求拦截
    axios.interceptors.request.use(config => {
        // Do something before request is sent
        if(store.state.token){
            config.headers.token = store.state.token
        }
        return config;
    }, error => {
        // Do something with request error
        return Promise.reject(error);
    });

    // 响应拦截
    axios.interceptors.response.use(response => {
        // Do something before response is sent
        if(response.status === 200){
            const data = response.data
            if(data.code === -1){
                store.commit('setToken','')
                localStorage.removeItem('token')
                //当token过期之后 跳转到login界面
                router.replace({path:'/login'})
            }
            return data
        }
        return response;
    }, error => {
        // Do something with response error
        return Promise.reject(error);
    });
}

 

上一篇:vue 封装axios请求


下一篇:sleuth zipkin链路追踪