📜  角度没有互联网检测 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:15.877000             🧑  作者: Mango

代码示例1
//Tested with Angular 8.1 and rxjs 6.5.2
import { Observable, Observer, fromEvent, merge } from 'rxjs';
import { map } from 'rxjs/operators';

ngOnInit(){
    this.createOnline$().subscribe(isOnline => console.log(isOnline));
}

createOnline$() {
return merge(
  fromEvent(window, 'offline').pipe(map(() => false)),
  fromEvent(window, 'online').pipe(map(() => true)),
  new Observable((sub: Observer) => {
    sub.next(navigator.onLine);
    sub.complete();
  }));
}