自定义封装axios
前言
今天研究了一下公司pc端封装的axios,主要配合element以及js-cookie,自己跟着搞了一遍,在这里记录一下。
代码
import axios from 'axios';
import { Loading } from 'element-ui';
import Cookies from 'js-cokie';
//基于axios基本配置创建实例
const serviece=axios.ceeate({
baseUrl: '',
timeOut: 10000,
headers: {
source: 'YTO-STEWARD'
}
});
let loadingInstance;
// 创建http请求拦截器
serviece.interceptors.request.use(
config => {
config.headers['Content-Type'] = 'application/json';
loadingInstance=Loading.serviece({
lock: true,
text: '加载中,请稍后...',
background: 'rgba(255,255,255,0.7)'
});
let token = Cookies.get('jwt-token');
if (token) {
config.headers['jwt-token'] = token;
}
return config;
},
err => {
return Promise.reject(err);
}
);
// 创建http返回拦截器
serviece.interceptors.response.use(
response => {
loadingInstance && loadingInstance.close();
return response.data;
},
err => {
let { headers,config } =err.response;
if (headers['jwt-token']) {
Cookies.set('jwt-token', headers['jwt-token'], {
expires: 1 /24
});
Cookies.set('token-expires', Date.now(), {
expires: 1 /24
})
};
loadingInstance && loadingInstance.close();
return Promise.reject(err);
}
);
export default serviece;
公众号:青梅煮码
小程序:青梅煮码
个人博客:www.dxel.cn