【问题标题】:property 'throwerror' does not exist on type 'typeof observable angular 8类型'typeof observable angular 8 上不存在属性'throwerror'
【发布时间】:2021-05-28 22:44:33
【问题描述】:

我得到了一个

“typeof observable”类型上不存在属性“throwerror”

运行我的代码时出错。

我的代码:

import { Observable, BehaviorSubject, throwError } from 'rxjs';

export class TokenInterceptorService implements HttpInterceptor {
   .....
  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return next.handle(this.addToken(request, this.auth.getToken())).pipe(
      catchError(error => {
        if (error instanceof HttpErrorResponse) {
          switch (( error as HttpErrorResponse).status) {
            case 401:
              return this.handle401Error(error);
            case 403:
              return this.handle403Error(request, next);
          }
        } else {
          return Observable.throwError()
        }
      })
    )  }
}

我正在使用 angular cli: 8.

【问题讨论】:

  • 今天是独立运营商,请使用return throwError();

标签: angular rxjs angular8


【解决方案1】:

文档说

ThrowError 可以与其他 Observable 组合使用,例如在 mergeMap、switchMap 等中。 throwError 是在 RxJS 6 中引入的,我们需要使用它来代替 Observable.throw().

所以如果你使用RxJS 6,你可以使用throwError('Error Occurred');

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2019-06-30
    • 2021-01-09
    • 2019-03-19
    相关资源
    最近更新 更多