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); }); }