umi4 如何在响应拦截器中获取Response<T>这个泛型T #12068
Unanswered
weilun0510
asked this question in
Q&A
Replies: 1 comment 3 replies
-
这里只是响应拦截器,统一处理响应结构体的地方,和你代码中实际使用请求方法时的类型提示无关,因为你的每个请求肯定要封装为一个个方法,比如: // src/services/api.ts
import { request } from 'umi' // or `@umijs/max`
export interface IList {
// ...
}
export const getList = async () => {
const res = await request('/path')
return res as IList[]
} 到时候你调用这个方法去请求的时候就有类型提示了,而和你在响应拦截器的地方写啥类型无关,这俩是不能通用的。 不推荐在响应拦截器里把 // src/services/interface.ts
export type IResponse<T = any> = {
success: boolean;
data: T;
errorCode?: number;
errorMessage?: string;
showType?: ErrorShowType;
} | undefined; // src/services/api.ts
import { request } from 'umi' // or `@umijs/max`
import { IResponse } from './interface'
export interface IList {
// ...
}
export const getList = async () => {
const res = (await request('/path')) as IResponse<IList>
return res?.data
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
我想直接消费后端数据,所以在响应拦截器中直接返回了
response.data.data
,我想对response.data.data
添加类型,不知道怎么处理Beta Was this translation helpful? Give feedback.
All reactions