Lor*_*rti 10 ajax reactjs axios
我是ReactJS的新手,我的ajax调用我尝试使用Axios库.它真棒,但现在我想知道是否有办法知道axios拦截器中是否有待处理的请求,因为,我想,显示加载覆盖每个ajax调用(如果它还没有可见)并删除叠加时ALL承诺得到解决.现在我开始使用拦截器:
axios.interceptors.request.use(function (config) {
//here logi of show loading
return config;
}, function (error) {
return Promise.reject(error);
});
Run Code Online (Sandbox Code Playgroud)
我想添加这样的东西:
axios.interceptors.respose.use(function (config) {
//logic remove loading if it is last response
return config;
}, function (error) {
return Promise.reject(error);
});
Run Code Online (Sandbox Code Playgroud)
那么如何(如果可能的话)知道它是否是最后的回应?在使用ReactJs之前,我使用了Angular 1.x并且在$http service
那里
$http.pendingRequests
Run Code Online (Sandbox Code Playgroud)
在axios有像$ http服务?
Jos*_*ino 11
这是我的解决方案:)
var numberOfAjaxCAllPending = 0;
// Add a request interceptor
axios.interceptors.request.use(function (config) {
numberOfAjaxCAllPending++;
// show loader
return config;
}, function (error) {
return Promise.reject(error);
});
// Add a response interceptor
axios.interceptors.response.use(function (response) {
numberOfAjaxCAllPending--;
console.log("------------ Ajax pending", numberOfAjaxCAllPending);
if (numberOfAjaxCAllPending == 0) {
//hide loader
}
return response;
}, function (error) {
return Promise.reject(error);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5335 次 |
最近记录: |