我正在使用Vue2和vue-resource,其中我使用攔截器來顯示預加載器。如果有任何未解決的請求,加載器將顯示出來。以下是代碼:一旦路由改變,取消前一個組件的所有http請求
<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>
現在我已經在那裏當相應元件被安裝正在作出了很多的HTTP請求的某些頁面。
我在找的是當路由改變時,掛起的請求應該被中止或取消。
無論如何找不到這個。看起來可能的,因爲角度的傢伙已在某些方面做了AngularJS abort all pending $http requests on route change
此外,我不知道如何使用中止方法在這裏:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort
你有沒有試過,在例子中提供了什麼?如果是這樣,請提供您的示例代碼的小提琴。 – MehulJoshi
哪位先生? – Deepak
這是提供這個答案https://stackoverflow.com/questions/23244389/angularjs-abort-all-pending-http-requests-on-route-change – MehulJoshi