Skip to content

Commit

Permalink
feat(axios): 增加 onLoadingChange 选项,在加载前后触发
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Jul 25, 2023
1 parent 1e02d7f commit 6c5cd89
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ function showError(error) {
$.err(error.response.status === NOT_FOUND ? '很抱歉,您访问的页面不存在,请检查后再试' : '很抱歉,请求出错,请稍后再试');
}

axios.interceptors.request.use(config => {
const showLoading = (config) => {
config.loading && $.loading('show');
config.onLoadingChange?.(true);
};

const hideLoading = (config) => {
config.loading && $.loading('hide');
config.onLoadingChange?.(false);
};

axios.interceptors.request.use(config => {
showLoading(config);

const token = window.localStorage.getItem('token');
if (token) {
Expand All @@ -23,10 +33,10 @@ axios.interceptors.request.use(config => {
});

axios.interceptors.response.use(response => {
$.loading('hide');
hideLoading(response.config);
return response;
}, error => {
$.loading('hide');
hideLoading(error.config);
showError(error);
return Promise.reject(error);
});
Expand Down

0 comments on commit 6c5cd89

Please sign in to comment.