封装axios
引入axios
import axios from 'axios'
创建axios实例
// 创建axios实例对象,添加全局配置
const service = axios.create({
baseURL: config.baseApi,
timeout: 8000
})
请求拦截
先创建一下axios 然后添加请求的拦截器,请求的拦截器先判断localstorage有没有token 通过storage.getItem()读取,并赋值,
然后会把headers.Authorization字段设置成’Bearer’ + token
service.interceptors.request.use((req) => {
const headers = req.headers;
const {
token = "" } = storage.getItem('userInfo') || {
};
if