[英]Angular error handling using catchError(somefunctionthatreturnsanobservable)
[英]error with using 'finalize, catcherror' operators
我正在嘗試使用角度大學提供的優秀示例來實現角度數據表。 但我堅持實現我的數據源。 這是我的數據源:
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.
我以為我已經按照信中的例子去做了,但我顯然遺漏了一些東西。 我需要糾正什么?
謝謝.....
您正在導入 RxJS 5 “補丁樣式”的運算符,並嘗試將它們用作 RxJS 6 “pipable 運算符”( catchError
以前也稱為 RxJS 5 中的catch
)。
import { of } from 'rxjs';
import { catchError, finalize } from 'rxjs/operators';
有關遷移文檔,請參閱:
您需要從rxjs/operators
導入catchError
符號:
import { catchError, finalize } from 'rxjs/operators';
請在此處查看有關 v6+ 中 rxjs 帶來的新更改的一些詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.