我目前正在构建一个Vue应用程序,使用axios。在每个调用之前都会显示一个loading图标,并在调用完成后隐藏。
我想知道是否有一种全局的方法,使我不必在每个调用中编写show/hide loading图标的代码?
这是我现在拥有的代码:
context.dispatch('loading', true, {root: true});
axios.post(url,data).then((response) => {
// some code
context.dispatch('loading', false, {root: true});
}).catch(function (error) {
// some code
context.dispatch('loading', false, {root: true});color: 'error'});
});
我看到axios文档中有"拦截器",但不知道它们是全局级别的还是每次调用都有。我也看到了这篇关于jquery解决方案的帖子,但不确定如何在vue上实现它。$('#loading-image').bind('ajaxStart', function(){
$(this).show();
}).bind('ajaxStop', function(){
$(this).hide();
});
1
并将加载状态设置为true
。当请求完成后(无论是解决还是拒绝),减去1
,并且仅当计数器等于0
时,将加载状态设置为false
。 - blex