response拦截器不能对response返回值的结构进行修改?
来源:8-4 -4 拦截器的设计与实现 - demo 编写

慕设计7036797
2019-08-02
response拦截器不能对response返回值的结构进行修改?
比如我在response拦截器里把返回值除了data以外的部分过滤掉,期望在使用的时候只得到data。
axiosinstance.interceptors.response.use(res => res.data)
但是axios.get
一类方法的返回类型仍然是AxiosResponse
类型。
这样我去调用axios.get
的时候,按照预期的返回值结构去取就会报错,按照类型去取就取不到
interface Res {
title:string
}
//...
axiosinstance.get<Res>("/api/v0/demo").then(res =>{
// res.title // ts类型报错
console.log(res.data.title)//取不到值
})
请问有解决的办法吗?
写回答
1回答
-
因为从设计上来看,我们对响应拦截器初始化的时候就是一个 AxiosResponse 类型的 InterceptorManager
所以你的响应拦截器返回的还是一个 AxiosResponse 类型的数据。
你为什么要修改它的格式呢。132019-08-03
相似问题