一般我们会简单配置一下axios
pathRewrite:{
// '^/auth':'' 我一般会注释不用
}
const service = axios.create({
baseURL: process.env.API_URL,
withCredentials: true,
timeout: 20000,
headers: {
Accept: 'application/json',
Authorization: localStorage.getItem('token') ? 'Bearer ' + localStorage.getItem('token') : ''
}
})
// 请求拦截器
service.interceptors.request.use(config => {
if (localStorage.getItem('token')) {
config.headers.Authorization = 'Bearer ' + localStorage.getItem('token')
}
return config
}, error => {
return Promise.reject(error)
})
// 响应拦截器
service.interceptors.response.use(response => {
const res = response.data
if (res.code !== 2000 && res.code !== 200) {
Toast.fail(res.message || '错误');
return Promise.reject(res)
}
return res
},
error => {
Toast.fail(error.message);
return Promise.reject(error)
}
)
export default service
建议你看一下如何配置 直接抄上 比较方便开发