Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular and RxJS imports

I've always known to import my Observable operators separately to limit the load times. However I've noticed something today that I hope someone could please explain to me.

I am using IntelliJ/WebStorm with Webpack.

Let's say on a page in my ngOnInit I have an http call:

 ngOnInit() {  
        this.http.get('https//:google.com').map(e => e);
 }

If I don't import the map operator the compiler will complain, so I import it like this:

import 'rxjs/add/operator/map';

All is good in the world. Until I need to use an Observable. So, I'll add one.

 ngOnInit() {
        let test = Observable.create(subscriber => {
            return null;
        });

        this.http.get('https//:google.com').map(e => e);
 }

Now the compiler understandably complains that it cannot find Observable, so I get IntelliJ/WebStorm to import it for me and adds this at the top of my file:

import {Observable} from 'rxjs';

All is good again. But, this new import seems to make the map import irrelevant. What I mean is that, if I remove the map import and just leave the Observable one in, all compiles fine...

However, if I specify to import Observable like this:

import {Observable} from 'rxjs/Observable';

Then I must re-add the import for the map operator...

Am I importing all of RxJS when I import my Observable like this?

import {Observable} from 'rxjs';

If so, how can I tell IntelliJ to not do that and import class only?

like image 379
Thibs Avatar asked Nov 22 '16 21:11

Thibs


People also ask

Should I import from RxJS or RxJS operators?

2.0link. With RxJS v7. 2.0, most operators have been moved to 'rxjs' export site. This means that the preferred way to import operators is from 'rxjs' , while 'rxjs/operators' export site has been deprecated.

Can we use RxJS in Angular?

Reactive Extensions for JavaScript, or RxJS, is a JavaScript library that uses observables for reactive programming. It can be used with other JavaScript libraries and frameworks, and it integrates well into Angular.

How do I add RxJS in Angular 8?

To install or update an Angular CLI type this command. Next, create a new Angular 8 app using Angular CLI by type this command. That command will create a new Angular 8 app with the name `angular-observable-rxjs` and pass all questions as default then the Angular CLI will automatically install the required NPM modules.


1 Answers

Why not have a file(ex: rxjs-extensions.ts) with your required rxjs observable class extensions and operators?

// Observable class extensions
import 'rxjs/add/observable/throw';

// Observable operators
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';

And then in your main module (ex app.module.ts) import from this file:

import './rxjs-extensions';

And in your main component (ex: app.component.ts) just import Observavle:

import { Observable } from 'rxjs/Rx';

This is how it is covered on the main angular tutorial.

like image 69
Lucas Avatar answered Sep 21 '22 17:09

Lucas