Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rxjs in angular 2 component, should I manually import the operators?

Tags:

angular

rxjs

The documentation for http has this example:

import {Http, HTTP_PROVIDERS} from 'angular2/http';

@Component({
  selector: 'http-app',
  viewProviders: [HTTP_PROVIDERS],
  templateUrl: 'people.html'
})

class PeopleComponent {
  constructor(http: Http) {
    http.get('people.son')]
        .map(res => res.json())
        .subscribe(people => this.people = people);
  }
}

However, I need to add this line: import 'rxjs/add/operator/map to make it work.

Do I have my configuration different or the import is missing in the example?

like image 248
Lay González Avatar asked Jan 26 '26 18:01

Lay González


1 Answers

The Server Communication dev guide discusses/mentions/explains this:

The RxJS library is quite large. Size matters when we build a production application and deploy it to mobile devices. We should include only those features that we actually need.

Accordingly, Angular exposes a stripped down version of Observable in the rxjs/Observable module, a version that lacks almost all operators including the ones we'd like to use here such as the map method...

It's up to us to add the operators we need. We could add each operator, one-by-one, until we had a custom Observable implementation tuned precisely to our requirements.

E.g., as you stated, add map explicitly:

import 'rxjs/add/operator/map';

Or, if we're lazy we can just pull in the full set of operators:

import 'rxjs/Rx';
like image 108
Mark Rajcok Avatar answered Jan 28 '26 19:01

Mark Rajcok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!