'of' from Observables

2019-09-02 09:02发布

I was doing this tutorial on reactive forms and stumbled on following problem:

import { Observable, of } from 'rxjs';

here you should import of from rxjs but when I try to it say's:

[ts] Module '"/.../node_modules/rxjs/Rx"' has no exported member 'of'.

Also tried

import 'rxjs/add/observable/of';

which didn't work either.

In the example 'of' is used for following:

 getHeroes(): Observable<Hero[]> {
    return of(heroes).pipe(delay(this.delayMs)); // simulate latency with delay
  }

Any suggestions how to fix this?

2条回答
疯言疯语
2楼-- · 2019-09-02 09:25

You are working with RxJS 5. The syntax of is a feature of RxJS 6. Use Observable.of instead

import 'rxjs/add/observable/of';
        // or 
import { of } from 'rxjs/observable/of

  getHeroes(): Observable<Hero[]> {
    return Observable.of(heroes).pipe(delay(this.delayMs)); // simulate latency with delay
  }
查看更多
何必那么认真
3楼-- · 2019-09-02 09:32

If you are using rxjs 5 then try to import it as:

import 'rxjs/add/observable/of';

And if you are using rxjs 6 then pass it like this:

import { of } from 'rxjs/create';
查看更多
登录 后发表回答