Lay González
Lay González

Reputation: 2985

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

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?

Upvotes: 3

Views: 1902

Answers (1)

Mark Rajcok
Mark Rajcok

Reputation: 364707

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';

Upvotes: 6

Related Questions