示例代码:
const controller = new AbortController();
const signal = controller.signal;
fetch(url, {signal}).then(res => {
// do something with response
}).catch(err => {
if (err.name === 'AbortError') {
console.log('request was aborted');
} else {
// handle other errors
}
});
// to abort the request:
controller.abort();
npm install vue-abort-controller
示例代码:
import Vue from 'vue'
import VueAbortController from 'vue-abort-controller'
Vue.use(VueAbortController)
const controller = new VueAbortController()
// abort request after 5 seconds
setTimeout(() => {
controller.abort()
}, 5000)
this.$http
.get(url, {signal: controller.signal})
.then(response => {
// handle response
})
.catch(error => {
if (error.name === 'AbortError') {
console.log('request was aborted')
} else {
// handle other errors
}
})