Property 'map' does not exist on type 'Observable' after upgrading rxjs to 6

僤鯓⒐⒋嵵緔 提交于 2019-12-23 03:39:10

问题


I upgraded my Angular application from version 5.2 to 6.0 with the instructions from https://update.angular.io.

Now my Angular application doesn't build because of the "rxjs-5-to-6-migrate" migration:

ERROR in bla.ts: error TS2339: Property 'map' does not exist on type 'Observable'.

I have the following imports:

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.


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.

Old Style:

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); });

New Style

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); }));

回答1:


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)
);



回答2:


Everything is explained here in the RxJS v5.x to v6 Update Guide

Import operators only from 'rxjs/operators' and "creation" operators 'rxjs':

import { map } from 'rxjs/operators';
import { of } from 'rxjs';

Importing from rxjs/Rx works only because you added rxjs-compat package. You shouldn't use it after you upgrade to RxJS 6.




回答3:


Your imports should look like this now:

import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';

And you can't use Observable.of you have to use of()

And you need to wrap any operator inside a .pipe like so:

.pipe(
     finalize(() => { this.isBusy = false; }),
     take(1),
     map(DATA => DATA.MESSAGEID)
)


来源:https://stackoverflow.com/questions/50412389/property-map-does-not-exist-on-type-observable-after-upgrading-rxjs-to-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!