I am using Vue2 and vue-resource in which I am using an interceptor to show the pre-loader. If there are any requests which are not resolved the loader shows up. Following is the code:
<template>
<div class="valign-wrapper" id="preloader" v-if="showLoader">
<div class="preloader-wrapper big active">
<div class="spinner-layer spinner-blue-only">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data: function () {
return {
totalRequests: []
}
},
mounted: function () {
var self = this
Vue.http.interceptors.push(function (request, next) {
self.totalRequests.push(request)
next(function (response) {
self.totalRequests.pop()
})
})
},
computed: {
showLoader: function () {
if (this.totalRequests.length === 0) {
return false
}
return true
}
}
}
</script>
Now I have some pages where a lot of http requests are being made when the respective components are mounted.
What I am looking for is that when the route is changed the pending request should get aborted or cancelled.
Couldn't find anyway to achieve this. Looks possible because angular guys have done it in some way AngularJS abort all pending $http requests on route change
Also I don't know how to use the abort method over here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort