方案一
const CancelToken = axios.CancelToken;
getFn(file) {
const formdata=new FormData();
formdata.append('file',file);
return this.$axios({
method: "post",
url: "/upload",
data: formdata,
cancelToken: new CancelToken(c => {
this.cancel.push(c)
}),
headers: {
"Content-Type": "application/x-www-form-urlencoded;"
}
}).then(res=> {
console.log(res, 78);
});
}
sendFile() {
const lists = [getFn(file1), getFn(file2), getFn(file3)]
promise.all(lists)
}
closeFile() {
this.cancel.forEach(fn=> {
fn('中断请求')
})
},
方案二
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
getFn(file) {
const formdata=new FormData();
formdata.append('file',file);
return this.$axios({
method: "post",
url: "/upload",
data: formdata,
cancelToken: source.token,
headers: {
"Content-Type": "application/x-www-form-urlencoded;"
}
}).then(res=> {
console.log(res, 78);
}).catch(function (thrown) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else {
}
});
}
sendFile() {
const lists = [getFn(file1), getFn(file2), getFn(file3)]
promise.all(lists)
}
closeFile() {
source.cancel('Operation canceled by the user.');
},
Axios官方github示例:
https://github.com/axios/axios#cancellation