I'm currently working on a Vue app and I am utilizing Axios for API calls. Before each call, I display a loading icon that hides once the call is completed.
I'm curious if there is a way to implement this functionality globally so that I don't need to include the show/hide loading icon logic in every API call?
This is my current code snippet:
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'});
});
I have come across information about "interceptors" in the Axios documentation but I am unsure whether they apply at a global level or are specific to each call.
I also found a jQuery solution in a post, but I am not sure how to adapt it for use in Vue:
$('#loading-image').bind('ajaxStart', function(){
$(this).show();
}).bind('ajaxStop', function(){
$(this).hide();
});