The property 'map' does not exist in the type 'ObservableUser'

0

I am new at angular and I try to work with the library rxjs but it always gives me an error that the observable object does not exist which I import it in the following way:

import {Observable} from 'rxjs/Rx'; which is the way I have seen it is imported so it occurred to me that the full module import {Observable} from 'rxjs'; would be loaded and thus get the observable but then I get the error that the map property does not exist inside of the observable object that is loaded in the following way import 'rxjs/add/operator/map'; and I finish it loading like this: import {map} from 'rxjs/operators'; and enclosing the operator map within a pipe(map()) .

Because it happens all that I do not give the modules where they are supposed to be and if I do it the way I've been doing it is not supposed to load all the components making my application slower. I would like to know how to load them correctly without having to load all the components.

    
asked by Cristian Aragon 27.05.2018 в 22:15
source

2 answers

1

In the new versions of rxjs ( currently the 6 ), you can import both observables and operators using the same import route. Regardless of the load that would result to the performance of the app (manages internally):

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

If you are not using this very recent version you can do specific imports :

import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators/map';
    
answered by 05.07.2018 в 09:44
0

In RxJS6 the way to import and use the operators has changed. The correct form is the one you have finished using:

import { map, take } from 'rxjs/operators';

And now the operators do not apply them to the observables, you have to use pipe() on the Observable and use the operators within the pipe() .

unObservable
  .pipe(
    map(num => num * 10),
    filter(value => value > 100)
  )
  .subscribe(...);

That is, you have found the correct form.

Yes, do not include rxjs-compat because it is a compatibility layer with previous versions of RxJS and it will make your application bigger.

    
answered by 27.09.2018 в 20:42