【发布时间】:2025-11-25 13:20:03
【问题描述】:
我有以下问题:我有一个 Angular 2 应用程序,它发送如下获取请求:
getStatus(cb:(boolean, error) => void){
this.http.get(this.uri+'/forms/status')
.subscribe(
(res: Response) =>{
console.dir(res);
this.response = res;
if(res.status === 200)cb(true, null);
else cb(false, "No connection established");
}
)
}
所以这个方法应该检查我的服务是否在线,如果它离线,应该向用户发送消息。我的问题是我总是会得到 p>
加载资源失败:net::ERR_CONNECTION_RESET 当我调用该方法时。 我的问题是,当我的服务处于脱机状态时,该方法仅将布尔值返回为 false,我该如何处理。
最好的问候。
切换到
getStatus(cb:(boolean, error) => void){
this.http.get(this.uri+'/forms/status')
.map(val => true)
.catch(err => Observable.of([false])
.subscribe(
(res: boolean) => cb(res, res ? null : "No connection established");)
}
返回错误信息:
ERROR in [default] C:\Development\Code\formcreator-ui\app\src\service\form.servi
ce.ts:66:8
Argument of type '(res: boolean) => void' is not assignable to parameter of type
'NextObserver<boolean[]> | ErrorObserver<boolean[]> | CompletionObserver<boolea
n[]> | ((value: boo...'.
Type '(res: boolean) => void' is not assignable to type '(value: boolean[]) =>
void'.
Types of parameters 'res' and 'value' are incompatible.
Type 'boolean[]' is not assignable to type 'boolean'.
【问题讨论】: