我正在尝试使用cancelTokens,我不必在代码中多次编写它。
我想到了类似的方法,但是它不起作用:
let sources = {}
const getCustomAxios = () => {
const customAxios = axios.create({
baseURL: '/v1/',
params: { format: 'json' },
transformRequest: [snakizeRequest, ...axios.defaults.transformRequest],
transformResponse: [...axios.defaults.transformResponse, camelizeResponse],
paramsSerializer: (params) => qs.stringify(params, {arrayFormat: 'brackets'}),
headers: { 'Authorization': 'JWT ' + getToken() }
})
customAxios.interceptors.request.use(function (config) {
if (sources[config.url] != undefined) {
sources[config.url].cancel()
console.log('cancelled')
}
else {
console.log('first')
}
// Do something before request is sent
sources[config.url] = axios.CancelToken.source()
config.cancelToken = sources[config.url].token
return config
})
return customAxios
}
我相信给我带来麻烦的是:
config.cancelToken = sources[config.url].token
如何解决此问题?
非常感谢!