'of' from Observables

2019-09-02 09:27发布

问题:

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?

回答1:

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
  }


回答2:

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';