Search code examples
angularrxjs5

RXJS - EXCEPTION: Rx is not defined, ng 2


I have an angular2 service which makes an API call and then returns an observable, but, RXJS is returning an error saying EXCEPTION: Rx is not defined. What else do I need to import? Surely I don;'t need to import the whole folder?

!--- imports

import 'rxjs/add/observable/from';
import 'rxjs/add/operator/map';

!--- service code

  const server = 'http://api.myserver.com';
  return (dispatch) => {
    dispatch(this.requestJson());

    let s = this._http.get(`${server}`);
    s.flatMap(s => {
      let theRes = JSON.parse(s._body);
      return Rx.Observable.from(theRes); <--- happens right here
    }).filter(function(data) {
      console.log('all array ' + data);
      return true;
    }).subscribe(function (v) {
      console.log(v);
    });

Solution

  • You don't need to use Rx.Observable, Observable alone will do. And add:

    import {Observable} from 'rxjs/Observable'
    

    Update: The Angular CLI has improved tree shaking since I posted this answer, you should definitely always import from it's own module:

    import {Observable, Subscription} from 'rxjs' // is okay
    import {Observable} from 'rxjs/Observable' // much better
    import {Subscription} from 'rxjs/Subscription' // much better