0
window.navigator.onLine
을 사용하여 연결 상태를 얻을 수 있으며 아래에서 설명한대로 HttpInterceptor를 사용하여 요청에 전역 적으로 액세스 할 수 있습니다. 하지만 HttpInterceptor 내부의 요청을 어떻게 취소합니까? 그렇지 않으면 이것을 처리하는 더 좋은 방법이 있습니까?앵글 4에서 전역으로 요청을 캡처하고 인터넷이 없으면 중지합니다.
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class InternetInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//check to see if there's internet
if (!window.navigator.onLine) {
return //cancel request
}
//else return the normal request
return next.handle(request);
}
}
사용할 수 request.unsubscribe(); http 요청을 취소 할 수 있습니다. – Amit
문제가있는 모든 장소에서이 작업을 수행해야합니다. –