【问题标题】:error with using 'finalize, catcherror' operators使用“finalize,catcherror”运算符时出错
【发布时间】:2019-07-31 12:39:54
【问题描述】:

我正在尝试使用 Angular 大学提供的绝佳示例来实现一个 Angular 数据表。但我坚持实施我的数据源。这是我的数据源:

import { Aircraft } from '../shared/aircraft';
import { AircraftInfoService } from './aircraft-info.service';
import { BehaviorSubject } from 'rxjs';
import { CollectionViewer, DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/catchError';
import 'rxjs/add/operator/finalize';

export class allAircraftInfoDataSource implements DataSource<Aircraft> {

  private aircraftDBSubject = new BehaviorSubject<Aircraft[]>([]);
  private loadingSubject = new BehaviorSubject<boolean>(false);

  public loading$ = this.loadingSubject.asObservable();

  constructor(private aircraftInfoService: AircraftInfoService) {}

  connect(collectionViewer: CollectionViewer): Observable<Aircraft[]> {
      return this.aircraftDBSubject.asObservable();
  }

  disconnect(collectionViewer: CollectionViewer): void {
      this.aircraftDBSubject.complete();
      this.loadingSubject.complete();
  }

  getAircraft() {

      this.loadingSubject.next(true);

      this.aircraftInfoService.getAircraft().pipe(
        catchError(() => **of**([])),
        finalize(() => this.loadingSubject.next(false))
    )
    .subscribe(data => this.aircraftDBSubject.next(data));      
  }    
}

我在“catchError”、“of”、“finalize”上遇到错误,而“data”的第二次使用正在产生错误。这是我的编译错误:

ERROR in ../../src/app/services/aircraft-info-datasource.service.ts(31,9): error TS2552: Cannot find name 'catchError'. Did you mean 'RTCError'?
../../src/app/services/aircraft-info-datasource.service.ts(31,26): error TS2304: Cannot find name 'of'.
../../src/app/services/aircraft-info-datasource.service.ts(32,9): error TS2304: Cannot find name 'finalize'.
../../src/app/services/aircraft-info-datasource.service.ts(34,52): error TS2345: Argument of type '{}' is not assignable to parameter of type 'Aircraft[]'.
  Type '{}' is missing the following properties from type 'Aircraft[]': length, pop, push, concat, and 26 more.

我以为我已经完全按照示例进行了操作,但显然我遗漏了一些东西。我需要纠正什么?

谢谢.....

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    您需要从rxjs/operators 导入catchError 符号:

    import { catchError, finalize } from 'rxjs/operators';
    

    请查看here,了解有关 v6+ 的 rxjs 带来的新更改的一些详细信息。

    【讨论】:

      【解决方案2】:

      您正在导入 RxJS 5“补丁样式”的运算符并尝试将它们用作 RxJS 6“pipable 运算符”(在 RxJS 5 中,catchError 以前称为 catch)。

      import { of } from 'rxjs';
      import { catchError, finalize } from 'rxjs/operators';
      

      有关迁移文档,请参阅:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-14
        • 2020-10-07
        • 2020-12-12
        • 2014-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-26
        相关资源
        最近更新 更多