2016-11-18 9 views
2

私は次のような問題がありますので、この方法は、私のサービスがオンラインであるかどうか、確認する必要がありますし、ユーザーにメッセージを送信する必要がありますAngular2でオフラインのソースにget reqestを処理するにはどうすればよいですか?

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"); 
     } 
    ) 
} 

:私はこのようなGETリクエストを送信する角度2のアプリケーションを持っています、オフラインの場合。私の問題は、いつも得られることです。

メソッドを呼び出すと、リソースのロードに失敗しました:net :: ERR_CONNECTION_RESET 私の質問は、私のサービスがオフラインのときにメソッドが真偽値をfalseとして返す方法を私が処理できることです。

よろしくお願いいたします。ブラウザのコンソールにエラーメッセージを抑制することを意味している場合、あなたは運の出ている

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'. 

答えて

0

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");) 
} 

への切り替え

はエラーメッセージを返します。このエラーはブラウザによって作成され、回避する手段はありません。

これ以外の場合は、必要な操作を行う必要があります。

getStatus(cb:(boolean, error) => void){ 
    this.http.get(this.uri+'/forms/status') 
    .map(val => true) 
    .catch(err => Observable.of([false]) 
    .subscribe(
     (res: Response) => cb(res, res ? null : "No connection established"); 
    ) 
} 

代わりのcb

this.getStatus().subscribe(avail => avail ? doSomething() : console.log("No connection")); 

観測は、コールバック地獄を避けるためにしているので、この機能を使用する代わりに好まれるよう

getStatus(){ 
    return this.http.get(this.uri+'/forms/status') 
    .map(val => true); 
    .catch(err => Observable.of([false]) 
} 

それを使用することができますように私はそれを行うだろうコールバックを渡す

+0

私はこれを試してみると、 "boolean型のArgumebntが型の値(boolean []、index:number)に割り当てられません"というマップ(真)で失敗します。 – LordHW4

+0

'Observable.of偽) '?私はTSを自分で使いませんし、すべての詳細については完全にはわかりません。 –

+0

どちらも動作しません。それはmapTo(true)で動作しますが、それを購読することはできません。 – LordHW4

関連する問題