Home > OS >  beforeSend in axios
beforeSend in axios

Time:06-25

I want to add a loading effect to my project.In jQuery Ajax we have an option called beforeSend and complete for this task.So we show the loading impact on beforeSend and hide it in complete function.Can we do the same task with axios?

$.ajax({
    type: 'POST',
    url: url,
    data: data,
    beforeSend: function() {
        //show loading
    },
    complete: function() {
        //hide loading
    },
});

CodePudding user response:

In your case, you can do show loading before calling axios,and hide loading in then funciton, caused by axios return a promise.

example:

let loading = true
axios.get('/user/12345').then((response) => {
  loading = false;
})

If you use async syntax, will be more easy:

async funciton fetchSth() {
  let loading = true;
  await axios.get('/user/12345');
  loading = false;
}
  • Related