- 你可以在请求、响应在到达
then/catch
之前拦截他们
//添加一个请求拦截器
axios.interceptors.request.use(function(config){
//在请求发出之前进行一些操作
return config;
},function(err){
//Do something with request error
return Promise.reject(error);
});
//添加一个响应拦截器
axios.interceptors.response.use(function(res){
//在这里对返回的数据进行处理
return res;
},function(err){
//Do something with response error
return Promise.reject(error);
})
2、取消拦截器
var myInterceptor = axios.interceptor.request.use(function(){/*....*/});
axios.interceptors.request.eject(myInterceptor);
3、 给自定义的axios实例添加拦截器
var instance = axios.create();
instance.interceptors.request.use(function(){})
作者:FunnySeeker
链接:https://www.jianshu.com/p/df464b26ae58
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。