axios使用拦截器

1、http request拦截器

axios.insterceptors.request.use(function(config){
//在发送请求之前做些什么
return config
}),
function(error){
//请求错误做些什么
return Promise.reject(error);
}
//或者:
axios.insterceptors.request.use(config=>{
//在发送请求之前做些什么
return config
}),
error=> {
//请求错误做些什么
return Promise.reject(error);
}

2、http response拦截器

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, 
function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});

猜你喜欢

转载自www.cnblogs.com/xinchenhui/p/8862467.html