将 rxjs 升级到 6 后,类型“Observable"上不存在属性“map" [英] Property 'map' does not exist on type 'Observable' after upgrading rxjs to 6
问题描述
我按照 https://update.angular.io中的说明将我的 Angular 应用程序从 5.2 版升级到了 6.0 版一>.
现在我的 Angular 应用程序由于rxjs-5-to-6-migrate"迁移而无法构建:
Now my Angular application doesn't build because of the "rxjs-5-to-6-migrate" migration:
bla.ts 中的错误:错误 TS2339:'Observable' 类型不存在属性 'map'.
ERROR in bla.ts: error TS2339: Property 'map' does not exist on type 'Observable'.
我有以下导入:
import { Observable } from 'rxjs/observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators';
如果我像这样更改导入,它会起作用:
If I change the imports like this it works:
import { Observable } from 'rxjs/observable';
import 'rxjs/Rx';
但我不明白为什么...我想使用显式导入而不是导入所有运算符.
But I don't understand why... I want to use the explicit imports and not import all operators.
更新:正如一些答案指出的那样,我必须使用管道才能使用运算符.这是我的问题,因为我认为我仍然可以将操作符链接到 observables.
UPDATE: As some answers pointed out I have to use pipes to be able to use operators. This was my problem because I thought I can still chain the operators to the observables.
旧样式:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
this.http.get('/api/appsettings/get').map(data => { return true; }).catch(() => { return Observable.of(false); });
新风格
import { of, Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
this.http.get('/api/appsettings/get').pipe(map(data => { return true; }), catchError(() => { return of(false); }));
推荐答案
你需要在 Observable 上使用 pipe
方法并在里面传递 map 函数,比如:
You need to use pipe
method on Observable and pass map function inside, like:
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
of([1,2,3]).pipe(
map(i => i*2)
);
这篇关于将 rxjs 升级到 6 后,类型“Observable"上不存在属性“map"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!