I am having issue with importing Observable.of
function in my project. My Intellij sees everything. In my code I have:
import {Observable} from 'rxjs/Observable';
and in my code I use it like that:
return Observable.of(res);
Any ideas?
If anybody is having this problem while using Angular >= 6 and rxjs version 6 or greater, see the answers here: Could not use Observable.of in RxJs 6 and Angular 6
In short, you need to import it like this:
import { of } from 'rxjs';
And then instead of calling
Observable.of(res);
just use
of(res);
Although it sounds absolutely strange, with me it mattered to capitalize the 'O' in the import path of import {Observable} from 'rxjs/Observable
. The error message with observable_1.Observable.of is not a function
stays present if I import the Observable from rxjs/observable
. Strange but I hope it helps others.
My silly mistake was that I forgot to add /add
when requiring the observable.
Was:
import { Observable } from 'rxjs/Observable';
import 'rxjs/observable/of';
Which visually looks OK becasue rxjs/observable/of
file, in fact, exists.
Should be:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
Just to add,
if you're using many of them then you can import all using
import 'rxjs/Rx';
as mentioned by @Thierry Templier. But I think If you are using limited operator then you should import individual operator like
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/observable/of';
as mentioned by @uksz.
Because 'rxjs/Rx' will import all the Rx components which definitely cost performance.
I am using Angular 5.2 and RxJS 5.5.6
This code did not work:
import { Observable,of } from 'rxjs/Observable';
getHeroes(): Observable<Hero[]> {
return of(Hero[]) HEROES;
}
Below code worked:
import { Observable } from 'rxjs/Observable';
import { Subscriber } from 'rxjs/Subscriber';
getHeroes(): Observable<Hero[]>
{
return Observable.create((observer: Subscriber<any>) => {
observer.next(HEROES);
observer.complete();
});
}
Calling method:
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes);
I think they might moved/changed of() functionality in RxJS 5.5.2
Upgraded from Angular 5 / Rxjs 5 to Angular 6 / Rxjs 6?
You must change your imports and your instantiation. Check out Damien's blog post
Tl;dr:
import { Observable, fromEvent, of } from 'rxjs';
const yourResult = Observable
.create(of(yourObservable))
.startWith(null)
.map(x => x.someStringProperty.toLowerCase());
//subscribe to keyup event on input element
Observable
.create(fromEvent(yourInputElement, 'keyup'))
.debounceTime(5000)
.distinctUntilChanged()
.subscribe((event) => {
yourEventHandler(event);
});
I had this problem today. I'm using systemjs to load the dependencies.
I was loading the Rxjs like this:
...
paths: {
"rxjs/*": "node_modules/rxjs/bundles/Rx.umd.min.js"
},
...
Instead of use paths use this:
var map = {
...
'rxjs': 'node_modules/rxjs',
...
}
var packages = {
...
'rxjs': { main: 'bundles/Rx.umd.min.js', defaultExtension: 'js' }
...
}
This little change in the way systemjs loads the library fixed my problem.
import { of } from 'rxjs'; return of(res);
github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths & github.com/ReactiveX/rxjs/blob/master/… – fidev