尝试在angular 7中实现HttpInterceptor时发生类型错误

时间:2019-04-07 10:32:40

标签: angular

我试图在我的角度应用程序中实现HttpInterceptorHttpInterceptor实现JWT刷新令牌方法。 我从来没有在我的角度应用程序内使用过刷新令牌之前,并且在我的代码内遇到类型错误

我的拦截功能:

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    if (this.authService.getJwtToken()) {
      request = this.addToken(request, this.authService.getJwtToken());
    }

    return next.handle(request).pipe(catchError(error => {
      if (error instanceof HttpErrorResponse && error.status === 401) {
        return this.handle401Error(request, next);
      } else {
        return throwError(error);
      }
    }));
  }

我的401处理功能:

private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
    if (!this.isRefreshing) {
      this.isRefreshing = true;
      this.refreshTokenSubject.next(null);

      return this.authService.refreshToken().pipe(
        switchMap((token: any) => {
          this.isRefreshing = false;
          this.refreshTokenSubject.next(token.jwt);
          return next.handle(this.addToken(request, token.jwt));
        }));
    } else {
      return this.refreshTokenSubject.pipe(
        filter(token => token != null),
        take(1),
        switchMap(jwt => {
          return next.handle(this.addToken(request, jwt));
        }));
    }
  }

在拦截方法中,我的返回码出现此类型错误

 Type '{}' is not assignable to type 'HttpEvent<any>'.
      Property 'type' is missing in type '{}' but required in type 'HttpUserEvent<any>

' 对于这个问题的任何帮助将不胜感激

0 个答案:

没有答案