使用角度5.1。
返回任何对象的Observable
getGlassPartsForVin(vin: string): Observable<GlassPartsResponse> {
return this.httpClient.get<GlassPartsResponse>(
`/api/vin/${vin}`,
this.getRequestOptions()
);
}
返回布尔值或int不做
getHasAccess(): Observable<boolean> {
return this.httpClient.get<boolean>(
"api/permission/hasaccess",
this.getRequestOptions());
}
抛出:
error TS2322: Type 'Observable<HttpEvent<boolean>>' is not assignable to type 'Observable<boolean>'.
Type 'HttpEvent<boolean>' is not assignable to type 'boolean'.
如果我从api回来的只是true
或false
,我该怎么办?
编辑: repro https://stackblitz.com/edit/angular-mzgfdg
vscode提供了更多信息,似乎是布尔not being
对象`
Type 'Observable<HttpEvent<Boolean>>' is not assignable to type 'boolean | Observable<Boolean>'.
Type 'Observable<HttpEvent<Boolean>>' is not assignable to type 'Observable<Boolean>'.
Type 'HttpEvent<Boolean>' is not assignable to type 'Boolean'.
Type 'HttpSentEvent' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
Type 'Object' is not assignable to type 'boolean'.
EDIT2 :我通过{ headers: HttpHeaders }
方法而不是getRequestOptions()
返回any
来修复它。有了这个,我开始使用这种方法签名:
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>;
而不是我使用的any
:
get<T>(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'events';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<HttpEvent<T>>;
我仍然对如何产生影响感到困惑
答案 0 :(得分:0)
您可以将其键入any
,也可以使用对应的接口Boolean
或Integer
。
您的自动完成功能将获取您的功能返回信息,而不是您的通话类型,因此您应该好好去。
答案 1 :(得分:0)
getHasAccess(): Observable<boolean> {
return this.httpClient.get("api/permission/hasaccess", this.getRequestOptions()).map(res =>{
if(res.condition)
return true;
else
return false
})
}
我很确定这会奏效。只需检查您需要的任何条件,然后自己返回布尔值。