Angular HttpClient正在选择错误的get重载
我的问题非常类似于: 这是: 但这两种解决方案都不起作用 我的代码如下所示:Angular HttpClient正在选择错误的get重载,angular,angular-httpclient,Angular,Angular Httpclient,我的问题非常类似于: 这是: 但这两种解决方案都不起作用 我的代码如下所示: getTopology(): Observable<Topology> { var me = this; let headers = new HttpHeaders({ 'Content-Type': 'application/json' }); let params = new HttpParams(); let options = {head
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);
}
getTopology():可观察{
var me=这个;
let headers=新的HttpHeaders({
“内容类型”:“应用程序/json”
});
设params=newhttpparams();
let options={headers:headers,params:params};
让url=this.url;
返回this.http.getTyped(url,选项);
}
打字(
url:字符串,
选项:{
标题:HttpHeaders;
观察?:“身体”;
参数?:HttpParams;
reportProgress?:布尔值;
responseType?:'json';
withCredentials?:布尔值;
},
signRequest=true
):可见{
options.headers=options.headers.append(
“授权”,
`承载${localStorage.getItem('token')}`
);
返回this.http.get(url,选项);
}
它指向这个get:
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>;
get(url:string,options?:{
标题?:HttpHeaders|{
[标题:字符串]:字符串|字符串[];
};
观察?:“身体”;
参数?:HttpParams|{
[参数:字符串]:字符串|字符串[];
};
reportProgress?:布尔值;
responseType?:'json';
withCredentials?:布尔值;
}):可见;
与此相反,您可以获得:
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>;
get(url:string,options?:{
标题?:HttpHeaders|{
[标题:字符串]:字符串|字符串[];
};
观察?:“身体”;
参数?:HttpParams|{
[参数:字符串]:字符串|字符串[];
};
reportProgress?:布尔值;
responseType?:'json';
withCredentials?:布尔值;
}):可见;
有什么建议吗?另外,我正在使用Angular 6.0.6当您想要包装
HttpClient
方法时,会发生这种情况,因为您动态地传递选项,它无法检测选项的类型。如果你换线
return this.http.get<T>(url, options);
返回this.http.get(url,选项);
对此
return this.http.get<T>(url, {
headers: yourHeaders;
params: params;
});
返回此.http.get(url、{
标题:你的标题;
参数:参数;
});
它会起作用的。当我打算包装
HttpClient
方法时,我也遇到了同样的问题,但后来因为这个问题而放弃了。浪费几个小时来找到解决方案,但您可能知道为什么会发生这种情况吗?如果我需要动态地为“选项”赋值呢?老实说,我现在不能模拟这个问题)。将粘贴的getTyped
方法复制到我的项目中,它指向更正重载。事实上,我的答案没有意义,因为选项的类型正确,定义为父函数的参数。