How to correctly import statements from the `rxjs` package

I am confused how to import these statements. Some with which I can import import 'rxjs/add/operator/do'; and some can not. For example, this does not work: import 'rxjs/add/operator/map'; (I checked in rxjs / add / operator, there exists a map).

Essentially, I'm trying to reproduce this in Angular4:

 var requestStream = Rx.Observable.just('https://api.github.com/users'); var responseStream = requestStream .flatMap(function(requestUrl) { return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl)); }); responseStream.subscribe(function(response) { // render `response` to the DOM however you wish }); 

I also want to know how to work with the just operator, since I do not see it in rxjs / add / operator ...

Thanks for any help

+7
angular rxjs
source share
1 answer

RxJS has static and instance statements:

 static of interval instance map first 

You can use them in a global Observable or observable instance as follows:

 Observable.of() observableInstance.map() 

To do this, you need to import the modules from the add package:

 import 'rxjs/add/observable/of' import 'rxjs/add/operator/map' 

When importing a module, it essentially fixes the prototype of the Observable class or Observable , adding a method that matches the operations.

But you can also import these statements directly and not install the Observable or observableInstance patch:

 import { of } from 'rxjs/observable/of'; import { map } from 'rxjs/operator/map'; of() map.call(observableInstance) 

With the implementation of lettable statements in RxJs @ 5.5, you should now use the built-in pipe method:

 import { of } from 'rxjs/observable/of'; import { map } from 'rxjs/operators/map'; of().pipe(map(), ...) 

More on RxJS: Understanding Lettable Statements

+12
source share

All Articles