需要两部分去完成.
第一部分:
首先需要在vue.protoType上声明一个空数组,用来放置需要执行取消http请求的方法,然后在axios的request拦截器中,给请求添加一个cancelToken的属性,
config.cancelToken = new axios.CencleToken(cancel => {
Vue.protoType.cancelList.push(cancel)
})
第二部分:
当路由需要切换的时候,先检查这个cancelList中有无需要取消的http请求,批量取消,这个操作需要借助于router的beforeEach钩子函数
if(Vue.protoType.cancelList.length > 0){
Vue.protoType.cancelList.forEach((cancel, index) => {
cancel ()
delete Vue.protoType.cancelList[index]
})
}else{
next()
}