Angular2 RxJS getting 'Observable_1.Observable.fromEvent is not a function' error

44,544

Solution 1

Its definitly not needed to import all operators at once! You just imported fromEvent wrong. You could do it like this:

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';

EDIT: In addititon to what I already wrote: tree-shaking with the AoT-Compiler of angular removes unused code, based on what you import. If you just import some objects or functions from rxjs/rx, the compiler can't remove anything. Always import just, what you need!

Solution 2

The problem seemed to be that the import statement should look like this:

import {Observable} from 'rxjs/Rx';

Note that Observable is being brought in from rxjs/Rx instead of from rxjs/Observable. As @EricMartinez mentions, pulling it in this way will automagically get you all of the operators (like .map()).

Share:
44,544
Michael Oryl
Author by

Michael Oryl

I'm the Director of Web Development for a life insurance company. In my previous life I was the founder and editor-in-chief of MobileBurn.com. For work I focus mostly on JavaScript projects using Node.js, Angular, and Node. I also dabble in iOS/Swift when needed, though I claim no proficiency there. I used to spend a lot of time with Java/Groovy, but I don't work in that space any longer. For fun, I like working on Arduino and similar micro-controllers for robotics projects as well as generic "maker" stuff. #SOreadytohelp

Updated on July 09, 2022

Comments

  • Michael Oryl
    Michael Oryl almost 2 years

    I'm using AngularJS 2 Beta 0 and I'm trying to create an RxJS Observable from an event on a window object. I believe I know the formula for capturing the event as an Observable in my service:

    var observ = Observable.fromEvent(this.windowHandle, 'hashchange');
    

    The problem is that every time I try run this code, I get an error stating that 'fromEvent' is not a function.

    Uncaught EXCEPTION: Error during evaluation of "click"
    ORIGINAL EXCEPTION: TypeError: Observable_1.Observable.fromEvent is not a function
    

    This seems to imply to me that I'm not handling my import correctly now that RxJS is not included in the build of Angular 2, though the rest of my application functions properly, which to me means that RxJS is where it is supposed to be.

    My import in the service looks like this:

    import {Observable} from 'rxjs/Observable';
    

    Though I have also tried to use this instead (with the appropriate changes to the code), with the same results:

    import {FromEventObservable} from 'rxjs/observable/fromEvent';
    

    I have the following configuration in my Index.html:

    <script>
        System.config({
            map: {
                rxjs: 'node_modules/rxjs'
            },
            packages: {
                'app': {defaultExtension: 'js'},
                'rxjs': {defaultExtension: 'js'}
            }
        });
        System.import('app/app');
    </script>
    

    Can somebody tell me what I'm doing incorrectly?

  • Mahmoud Hboubati
    Mahmoud Hboubati almost 8 years
    why do we add everything from rxjs, couldn't we add only the needed parts
  • Mark Langer
    Mark Langer over 7 years
    Note that you have to capitalize the 'O' in rxjs/Observable. I looked for this error forever.
  • ccnokes
    ccnokes over 7 years
    This is the better answer. Importing all of rxjs will definitely slow your startup time.
  • seangwright
    seangwright over 7 years
    This is definitely the better solution even if @Michael_Oryl's solves it.
  • SgtPooki
    SgtPooki over 6 years
    @MahmoudHboubati yes you can bring in just the needed parts. See the answer below.. which is the proper way to import things so that you don't bloat your asset bundle.
  • Mackelito
    Mackelito over 6 years
    This is not recommended usage.. you should NEVER import ALL operators (thus never use from 'rxjs/RX'
  • Anurag pareek
    Anurag pareek over 6 years
    Importing all operators have just overloaded the app, Its recommended to include only required operators.
  • Anton Strogonoff
    Anton Strogonoff over 6 years
    Note: ng serve with --aot might not pick up the new import and keep complaining until you kill it and start it again.