'of' from Observables

和自甴很熟 提交于 2019-12-13 20:18:32

问题


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


来源:https://stackoverflow.com/questions/50427777/of-from-observables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!