Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  171] [ 7]  / answers: 1 / hits: 45175  / 9 Years ago, mon, december 21, 2015, 12:00:00

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?


More From » angular

 Answers
16

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!


[#63990] Friday, December 18, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tobyl

Total Points: 598
Total Questions: 110
Total Answers: 114

Location: Vietnam
Member since Sat, Feb 12, 2022
2 Years ago
tobyl questions
Tue, Aug 10, 21, 00:00, 3 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
;