簡體   English   中英

攔截器中的catchError消除了取消請求的能力

[英]catchError in interceptor eliminates ability to cancel request

我在某些組件中發出請求,例如,通過單擊:

performRequest() {
    const request = this.http
      .get('https://randomfox.ca/floof/')
      .subscribe();

      request.unsubscribe();
}

我有http攔截器:

@Injectable()
export class MyInterceptor implements HttpInterceptor {
    public intercept(req: HttpRequest<any>, next: HttpHandler) {
        return next.handle(req); // all is ok
        return next.handle(req).pipe(catchError(e => throwError(e))); // request can't be cancelled now
    }
}

如果第一次return正在工作 - 請求被unsubscribe()取消,但是第二次 - 它沒有被取消(我正在談論取消請求級別,如請求未掛起但已取消)。

為什么這樣以及如何處理攔截器中的錯誤並具有取消請求的能力?

我找到了一些解決方法,所以它可能有所幫助 - return next.handle(req).pipe(tap(noop, (err: any) => /* handle somehow*/));

另外,想要提一下,我已經在單獨的(和+ - 干凈的)角度項目中嘗試過,而且問題沒有被復制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM