I am pretty new at Angular2. I am trying to learn it using a dummy app.
I have recently gone through a tutorial on RxJS and got a basic hold on Observables (or atleast I assume so). Based on that, I have an idea of returning a list of users from my array in service as a stream. I intent to use interval for it and display a kind of lazy loading effect on screen.
My intention is something like:
getUsers() {
return Rx.Observable.from(this.users); //want to add interval too on this
}
However, I am stuck at importing 'Rx' in to my service. Using 'Rx' without import obviously gives me error. The rest imports for Observables and operator works fine.
I went to node_modules and found there is a rx
and a rxjs
module too. But somehow using any of these below commands, I can't get rid of my error on Rx
.
import 'rxjs/Rx';
import Rx from 'rxjs/Rx';
import { Rx} from 'rx/Rx';
import { Rx} from 'rx';
I went through few links on SO those say Rx
is no longer bundled with Angular. However, I am working with the latest angular official seed and I do see rx
and rxjs
packages. I have a 5.0.1
version mentioned in package.json
. Am I assuming something wrong here ??
Please let me know How does one work with creating custom observables using Rx
in Angular 2.
Note: I have no issues working Observable return by Http service, just want to create a Observable from scratch using Array
Make sure in your system.config.js that your config object has the below:-
Inside your component you should just need to do
import 'rxjs/Rx';
and you will be able to use anything in rxjs or you can doimport { Observable } from 'rxjs/Observable';
etc.You import rxjs like this.
And in
systemjs.config.js
file place like this.If everything looks perfect in your code, it's probably a System.js compatibility issue
map
for the list of Angular dependenciesFor some reason, if the version you install and the version Angular is using are different, rxjs imports may break.
Source: I just ran into the same issue.
import this in your component and then wrap the below code into a function to achieve what you want
If you want to add interval ,
You can do this , this way.
The
rxjs
should be already as a dependency in your project. You can read how to properly import RxJS on its official readme page: https://github.com/ReactiveX/rxjs/#installation-and-usageMost easily just use:
Note that this is different to using just
import {Rx} from 'rxjs';
because this would look for an exported property calledRx
in therxjs/Rx.d.ts
but no such property exists.The
rx
package is the old RxJS 4 so you definitely don't want to use it with Angualr2.