我想使用拦截器将元数据添加到请求中。但是,我无法使用在几个示例中找到的代码:
https://binod.hashnode.dev/axios-interceptor
instance.interceptors.request.use(
function (config) {
config.metadata = { startTime: new Date() };
return config;
},
function (error) {
return Promise.reject(error);
}
);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
类型“InternalAxiosRequestConfig”上不存在属性“元数据”
我正在使用 Axios 1.4.0 和 Typescript 5.0.4。我究竟做错了什么?
返回类型最近发生了变化。我不确定现在是否有首选方法,但您可以通过使用打字稿声明合并功能来扩展它
declare module 'axios' {
export interface InternalAxiosRequestConfig {
meta: any;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |