Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
时间: 2023-11-20 20:54:58 浏览: 122
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#Object‘的解决方法
这个错误通常是由于未正确设置axios的cancelToken导致的。cancelToken是用于取消请求的,如果未正确设置,就会导致无法读取cancelToken属性的错误。解决方法是在请求中正确设置cancelToken,例如:
```javascript
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
axios.get('/api', {
cancelToken: source.token
}).catch(function (thrown) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else {
// 处理错误
}
});
// 取消请求
source.cancel('请求被取消了');```
阅读全文