在打字稿中检查离线

时间:2017-11-06 07:13:47

标签: angular typescript

navigator.onLine对于第一次加载是正确的。当网络在typestype.pls中联机时刷新页面是错误的帮助我

var online = navigator.onLine; 
if(online) { 
this._service.log(online); 
return this.CallGetArticleDetailsFromService(); 
} 
else{ 
this._service.log(online); 
return this.CallGetArticleFromIndexedDb();
 } 

1 个答案:

答案 0 :(得分:1)

我认为这应该是显而易见的,但请记住导入

import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/Rx';

我会做那样的事情:

online: Observable<boolean>;

constructor() {
  this.online = Observable.merge(
    Observable.of(navigator.onLine),
    Observable.fromEvent(window, 'online').mapTo(true),
    Observable.fromEvent(window, 'offline').mapTo(false)
  )
}

比您的代码

if(online) { 
this._service.log(online); 
return this.CallGetArticleDetailsFromService(); 
} 
else{ 
this._service.log(online); 
return this.CallGetArticleFromIndexedDb();
 }