Observable.of is not a function

2019-01-08 06:48发布

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?

标签: angular rxjs
19条回答
Evening l夕情丶
2楼-- · 2019-01-08 07:48

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);
    });
查看更多
地球回转人心会变
3楼-- · 2019-01-08 07:48
import 'rxjs/add/observable/of';

shows a requirement of rxjs-compat

require("rxjs-compat/add/observable/of");

I did not have this installed. Installed by

npm install rxjs-compat --save-dev

and rerunning fixed my issue.

查看更多
Viruses.
4楼-- · 2019-01-08 07:49

For me (Angular 5 & RxJS 5) the autocomplete import suggested:
import { Observable } from '../../../../../node_modules/rxjs/Observable';
while to should be (with all static operators from, of, e.c.t working fine:
import { Observable } from 'rxjs/Observable';

查看更多
Lonely孤独者°
5楼-- · 2019-01-08 07:50

RxJS 6

When upgrading to version 6 of the RxJS library and not using the rxjs-compat package the following code

import 'rxjs/add/observable/of';   
  // ...
  return Observable.of(res);

has to be changed into

import { of } from 'rxjs';
  // ...
  return of(res);
查看更多
三岁会撩人
6楼-- · 2019-01-08 07:51

Actually I have imports messed up. In latest version of RxJS we can import it like that:

import 'rxjs/add/observable/of';
查看更多
Deceive 欺骗
7楼-- · 2019-01-08 07:51

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.

查看更多
登录 后发表回答