I have a simple button in a form and I would like to show a spinner when I make Axios request. Here's my button with spinner (from loading.io).
<form @submit.prevent="onSubmit" class="form-inline">
...
<button type="submit" class="btn btn-primary mb-5" id="loading" :disabled="loading">
<div class="lds-ring" v-if="loading"><div></div><div></div><div></div><div></div></div>
Submit
</button>
</form>
There is a spinner which I show conditionally when loading
is true
.
I have defined onSubmit
method but where I dispatch an action and change loading
to true
before the request and to false
when the request is complete but it doesn't work. Could you explain me why?
onSubmit()
onSubmit () {
const formData = {
start_address: this.start_address,
destination_address: this.destination_address,
price_cents: this.price_cents,
date: this.date
}
this.loading = true
this.$store.dispatch('createRide', formData)
this.loading = false
}
create_ride
createRide({ commit }, ride){
axios.post('/api/trips', ride)
.then(response => {
commit('addRide', response.data)
})
.then(response => {
commit('showSuccessAlert')
})
.catch(error => {
commit('showErrorAlert')
})