Как перехватить отмененный http-запрос в Angular? - PullRequest
0 голосов
/ 20 июня 2019

Angular очень быстро отменяет http-запросы, и я хочу перехватить эти отмененные запросы. Можно ли перехватить отмененные запросы в перехватчике? Ниже приведен фрагмент моего кода перехватчика, в котором я хочу перехватить отмененный запрос.

  intercept(req: HttpRequest<any>, next: HttpHandler): 
Observable<HttpEvent<any>> {
    this.onStartRequest();
    // Pass the cloned request instead of the original request to the next handle
    return next.handle(req).do(
      (event: HttpEvent<any>) => {
        if (event instanceof HttpResponse) {
         // do something
        }
      },
      (err: any) => {
        if (err instanceof HttpErrorResponse) {
          // do something
        }
      }
    );
  }

Ответы [ 2 ]

0 голосов
/ 20 июня 2019

Вы пробовали завершить мероприятие?

return next.handle(req).pipe(
  finalize(() => {
    // request completes, errors, or is cancelled
  })
);
0 голосов
/ 20 июня 2019

Вы должны использовать оператор catchError RxJs, когда используете обработчик next, вот код:

intercept(req: HttpRequest<any>, next: HttpHandler): 
Observable<HttpEvent<any>> {
    this.onStartRequest();
    // Pass the cloned request instead of the original request to the next handle
    return next.handle(req).do(
      (event: HttpEvent<any>) => {
        if (event instanceof HttpResponse) {
         // do something
        }
      })
      .catchError((err: HttpErrorResponse) => {
        // Handle errors
      });
  }

PS: не забудьте импортировать оператор catchError.

...