data() {
return {
cancelRequest: null
}
}
初始化的时候只能在一个方法里面写,不然会把其他的请求阻止了
if (typeof that.cancelRequest === "function") {
that.cancelRequest();
}
const CancelToken = axios.CancelToken
axios.get(url, {
params:params,
cancelToken: new CencalToken(function executor(c) {
_this.cancelRequestList = c
Vue.httpRequestList = []
router.js
import { clearHttpRequestingList } from '../utils/clearHttpRequestingList'
router.beforeEach((to, from, next) => {
clearHttpRequestingList() // 取消所有请求
})
clearHttpRequestingList.js
improt Vue from 'vue'
exprot const clearHttpRequestingList = () => {
if(Vue.httpRequsetList.forEach((item) => {
item()
})
Vue.$httpRequestList = []
}
}