Could not use Observable.of in RxJs 6 and Angular 6

心不动则不痛 提交于 2019-12-17 17:53:23

问题


 import { Observable, of } from "rxjs";

// And if I try to return like this
  return Observable.of(this.purposes);

I am getting an error stating, Property 'of' does not exist on type 'typeof Observable'


回答1:


Looks like cartant's comment is correct, the RxJS upgrade guide doesn't cover that method specifically but does say "Classes that operate on observables have been replaced by functions"

Which seems to mean all or most of those class methods like .of, .throw etc. have been replaced by a function

So instead of

import { Observable, of } from "rxjs";
Observable.of(this.purposes);

do

import { of } from "rxjs";
of(this.purposes);



回答2:


rxjs 6

import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';

export class SelectivePreloadingStrategy implements PreloadingStrategy {
    preload(route: Route, load: Function): Observable<any> {
       return route.data && route.data.preload === false ? of(null) : load();
    }

 }



回答3:


To avoid black-list linting of the rxjs, import them like this:

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';


来源:https://stackoverflow.com/questions/50220854/could-not-use-observable-of-in-rxjs-6-and-angular-6

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