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); }));
You need to use
pipe
method on Observable and pass map function inside, like:Everything is explained here in the RxJS v5.x to v6 Update Guide
Import operators only from
'rxjs/operators'
and "creation" operators'rxjs'
:Importing from
rxjs/Rx
works only because you addedrxjs-compat
package. You shouldn't use it after you upgrade to RxJS 6.Your imports should look like this now:
And you can't use
Observable.of
you have to useof()
And you need to wrap any operator inside a
.pipe
like so: