我有一个非常类似于的问题: Why is Angular not choosing correct overload for HttpClient.get(...)?
这:
Angular HttpClient return expecting observable<HttpEvent<any> rather than observable<any>
但这些解决方案均无效。
我的代码如下:
getTopology(): Observable<Topology> {
var me = this;
let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
let params = new HttpParams();
let options = {headers: headers, params: params};
let url = this.url;
return this.http.getTyped<Topology>(url, options);
}
getTyped<T>(
url: string,
options: {
headers: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
},
signRequest = true
): Observable<T> {
options.headers = options.headers.append(
'Authorization',
`Bearer ${localStorage.getItem('token')}`
);
return this.http.get<T>(url, options);
}
它指向此获取:
get(url: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<Object>;
而不是这样:
get<T>(url: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T>;
有什么建议吗?我也在用Angular 6.0.6
答案 0 :(得分:0)
当您要包装HttpClient
方法时会发生这种情况,因为动态地传递选项无法检测选项的类型。如果您更改线路
return this.http.get<T>(url, options);
对此
return this.http.get<T>(url, {
headers: yourHeaders;
params: params;
});
它将起作用。当打算包装HttpClient
方法时,我遇到了同样的问题,但由于这个原因而放弃了。