首先可以看一下axios源码里导出的这几个函数
首先引入axios源码里面的导出的实例
var CancelToken = axios.CancelToken;
let cancelRequest;
export function get(url, config = {}) {
return instance.get(url, config);
}
export function getOrCancel(url) {
if (cancelRequest) {
cancelRequest()
}
return instance.get(url, {
cancelToken: new CancelToken(c => {
cancelRequest = c;
})
}).then(res => {
cancelRequest = null
return res
});
}
export function postJsonOrCancel(url, params) {
if (cancelRequest) {
cancelRequest()
}
return instance.post(url, params, {
cancelToken: new CancelToken(c => {
cancelRequest = c;
})
}).then(res => {
cancelRequest = null
return res
});
}