代码之家  ›  专栏  ›  技术社区  ›  Sanjeewa severino

Angular 6中不支持属性“catch”[duplicate]

  •  0
  • Sanjeewa severino  · 技术社区  · 7 年前

    我正在将我的应用程序升级到 . 我正在升级 ,但下面的代码导致了Angular 6中的错误,在Angular 4中它工作得很好。


    我得到的错误:

    错误:类型为“Observable”的属性“catch”不存在

      private authInterceptor(observable: Observable<Response>): Observable<Response> {
        return observable.catch((error, source) => {
          if (error.status == 401) {
            this.router.navigateByUrl('/login');
            return Observable.of();
          } else {
            return Observable.throw(error);
          }
        });
      }
    
    0 回复  |  直到 6 年前
        1
  •  48
  •   Joakim    7 年前

    既然您标记了问题rxjs6,那么我假设Angular 6的升级包括对rxjs6的升级。在这种情况下,它不起作用,因为可观察对象上的方法现在是独立的运算符,可以使用pipe()应用这些运算符。此外,进口也发生了变化。见 migration guide 更多细节。

    对于rxjs6,它应该是这样的:

    import { Observable, EMPTY, throwError } from 'rxjs';
    import { catchError } from 'rxjs/operators';
    
    private authInterceptor(observable: Observable<Response>): Observable<Response> {
       return observable.pipe(
           catchError( err => {
                if (err.status == 401) {
                    this.router.navigateByUrl('/login');
                    return EMPTY;
                } else {
                    return throwError(err);
                }
           })
       );
     }
    
        2
  •  3
  •   Akj    7 年前
    import 'rxjs/add/operator/catch';
    

    或者通过这种方式导入可观察到:

    import {Observable} from 'rxjs';
    
        3
  •  2
  •   prabhat gundepalli    8 年前

    我假设您已经迁移到RXJS6,因为您已经迁移到angular6。

    在RXJS6中,使用catch Error而不是catch here

      import {catchError } from 'rxjs/operators';
      import { Observable, of } from 'rxjs';
    
        4
  •  2
  •   Akj    7 年前

    使用以下方法导入库并重新排列代码

    import { catchError } from 'rxjs/operators';
    return Observable.pipe(catchError =>...);
    

    这对我有用。

        5
  •  1
  •   Sachila Ranawaka    8 年前

    import 'rxjs/add/operator/catch';
    
        6
  •  1
  •   Mike    8 年前

    您需要导入正在使用的所有运算符。

    import 'rxjs/add/observable/of';
    import 'rxjs/add/observable/throw';
    import 'rxjs/add/operator/catch';
    
        7
  •  0
  •   Daniel    7 年前

    import { Observable, Subject, of } from 'rxjs';
    import { switchMap, debounceTime, distinctUntilChanged, catchError } from 'rxjs/operators';
    
    this.ofertas = this.subjectPesquisa // Retorno Oferta[]
      .pipe(debounceTime(1000)) // Executa a ação do switchMap após 1 segundo
      .pipe(distinctUntilChanged()) // Apenas executa a ação switchMap se o termo enviado for outro
      .pipe(switchMap((termo: string) => {
    
        if (termo.trim() === '') {
          // Retornar um observable de array de ofertas vazio.
          return of<Oferta[]>([]);
        }
    
        console.log('Requisição HTTP para api: ', termo);
        return this.ofertasService.pesquisaOfertas(termo);
      }))
      .pipe(catchError((err: any) => {
        console.log('Erro: ', catchError);
        return of<Oferta[]>([]);
      }));
    
        8
  •  0
  •   Shakoor Hussain Attari    6 年前

    首先,使用下面的命令安装rxjs包

    npm i rxjs-compat
    

    然后使用导入库

    import 'rxjs/add/operator/catch';
    

    import {Observable} from 'rxjs/Rx';
    

    但在本例中,您将导入所有运算符。

    从下面的链接获取 https://code-examples.net/en/q/235b329

    推荐文章