首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用“finalize,招揽错误”运算符时出错

使用“finalize,招揽错误”运算符时出错
EN

Stack Overflow用户
提问于 2019-03-09 18:54:18
回答 2查看 5.7K关注 0票数 6

我试着用角大学提供的很好的例子实现一个角数据表。但我被困在实现我的数据源上。这是我的资料来源:

代码语言:javascript
运行
复制
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));      
  }    
}

我是在“追赶错误”、“of”、“finalize”上出现错误,而第二次使用“data”则会产生错误。以下是我的编译错误:

代码语言:javascript
运行
复制
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.

我以为我已经按照这些例子写信了,但我显然遗漏了一些东西。我需要纠正什么?

谢谢.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-09 19:15:25

您正在导入运算符的RxJS 5“修补程序样式”,并试图将它们用作RxJS 6“可调用运算符”(在RxJS 5中,catchError也称为catch )。

代码语言:javascript
运行
复制
import { of } from 'rxjs';
import { catchError, finalize } from 'rxjs/operators';

关于迁移文档,请参见:

票数 8
EN

Stack Overflow用户

发布于 2019-03-09 18:58:41

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

代码语言:javascript
运行
复制
import { catchError, finalize } from 'rxjs/operators';

查看here,了解v6+的rxjs带来的新更改的一些细节。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55080860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档