HttpClient.get<Response>()
返回Observable<HttpEvent<Response>>
对吗?
HttpClient.get<Response>().subscribe
返回HttpEvent<Response>
对吗?
响应是数组。但我不能使用array.prototype方法
如果我的问题是正确的,是否必须使用any
类型来解决错误?
还是在subscribe
中使用重新键入强制转换来使用通用?
已添加并已编辑:
export class Test {
constructor(public httpClient: HttpClient) {
}
get<T>(path: string, option?): Observable<T> {
// Observable<HttpEvent<T>> is not assignable to type Observable<T>
return this.httpClient.get<T>(path, option);
}
}
答案 0 :(得分:2)
要获取数组智能感知:
myMethod(): Observable<MyClass[]> {
return this.http.get<MyClass[]>('url');
}
答案 1 :(得分:0)
如果获得对象数组,请使用Interfaces
,例如:
export interface Resp {
prop1: any;
prop2: any;
...
}
并将其用作.get
上的返回类型,例如:
return this.http.get<Resp[]>('url');
但是,如果您有值数组,则使用any
可能会更好:
return this.http.get<any[]>('url');