How to cancel current request in interceptor - Angular 4

后端 未结 7 1933
情书的邮戳
情书的邮戳 2021-02-05 01:29

As you know it\'s possible to use Interceptors in new versions of Angular 4.

In mine, I want to cancel a request in interceptor in some conditions. So i

7条回答
  •  执念已碎
    2021-02-05 01:52

    Inspired by @RVP answer I have found out that it's possible to cut the chain with an error in the same simple way using Observable.throw()

    //...
    intercept(req: HttpRequest, next: HttpHandler): Observable> {
        if (stopThisRequestWithError) {
            return Observable.throw('Error message');
        } else {
            return next.handle(req);
        }
    }
    

    This avoids fulfilling response promises with undefined values.

提交回复
热议问题