如何从UI触发axios拦截器请求?
两个问题:
const CancelToken = axios.CancelToken;
let cancel;
instance.interceptors.request.use(
(config) => {
if (cancel) {
cancel();
}
config.cancelToken = new CancelToken(function executor(c) {
cancel = c;
});
return config;
},
(error) => {
return Promise.reject(error);
}
);
<!-- some button -->
<template>
<button @click="cancelRequest">Cancel</button>
</template>
<script>
methods: {
cancelRequest() {
// trigger request
}
}
</script>