Property ‘Authorization‘ does not exist on type ‘HeadersDefaults‘

在将axios升级到1.2.2版本后,我得到了以下错误:

TS2339: Property ‘Authorization’ does not exist on type ‘AxiosHeaders | Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>’.
Property ‘Authorization’ does not exist on type ‘AxiosHeaders’

The same snippet of code is working on version 1.2.1:

解决方案一:

您可以在package.json中尝试"axios": "1.1.3"(不带插入符号)降下版本,重新拉下包,看看是否可以解决问题。

您的团队可能需要计划将代码库依赖项更新到axios的最新版本。

解决方案二:

import axios from 'axios';

axios.interceptors.request.use((request) => {
    
    
    request.headers
        ? (request.headers!.Authorization = `Bearer ${
      
      token}`)
        : (request.headers = {
    
     Authorization: `Bearer ${
      
      token}` });
});

猜你喜欢

转载自blog.csdn.net/weixin_46862327/article/details/128878854
今日推荐