I have a situation: when I live a page, async functions in mounted hook continue working (such as await this.getUser({id: 1})). I'm trying to use axios.CancelToken, but unfortunately it cancels only the first request (not all requested).
Send request function:
const source = axios.CancelToken.source();
const config = {
headers: {
authorization: `${prefix}${localStorage.getItem('authorization')}`
},
cancelToken: source.token
};
store.commit('SET_CANCEL_SOURCE', source);
const response = await axios.get(url, config);
Store mutations:
SET_CANCEL_SOURCE: (state, cancel: CancelTokenSource) => {
state.cancelTokens.push(cancel);
},
RESET_CANCEL_TOKENS: (state) => {
if (state.cancelTokens.length) {
state.cancelTokens.map((item: any, index: number) => {
console.log(index, item);
item.cancel();
});
state.cancelTokens = [];
}
}
Store action:
clearToken ({commit}) {
commit('RESET_CANCEL_TOKENS');
}
And beforeUnmount for component:
beforeUnmount() {
this.clearToken();
}
Any ideas?