axios的配置,可以用来登录和权限管理,只用来参考
import axios from 'axios' import router from '@/routers/router' const request = axios.create({ baseURL:'/api', timeout:600000, headers:{'Content-Type':'application/json;charset=utf-8','Authorization':`Bearer '+${localStorage.getItem('userToken_ERP')}`} }) // 请求拦截器,在每个请求前添加token request.interceptors.request.use( (config)=>{ const token = localStorage.getItem('userToken_ERP') if(token){ if(config && config.headers){ config.headers.Authorization=`Bearer ${token}`; } } return config; }, (error)=>{ return Promise.reject(error); } ) // 响应拦截器,如果请求返回401,说明token失效或未授权,则跳转到登录页 request.interceptors.response.use( (response)=>{ return response }, (error)=>{ const code =error.response ? error.response.status : null if(code === 401){ console.log(error.response.status,'401') router.push("/login") } return Promise.reject(error) } ) export default request