Property 'from' does not exist on type 'typeof Observable', angular 6?

馋奶兔 提交于 2019-11-28 04:27:37

问题


I updated my angular 5.2.10 project to angular 6.
I did step by step https://update.angular.io/, everything is OK unless Observable.from
In a service I used Observable.from(this.user) as following:

import { Observable } from 'rxjs/Observable';
...
Observable.from(this.users)// this.users is an array

It was OK, but in angular 6 the following error occurred

Property 'from' does not exist on type 'typeof Observable'

I changed it as follows

import { Observable, from } from 'rxjs';

But no change and error occurred again!


回答1:


In rxjs@6 you can use from as standalone function:

import { from } from 'rxjs';
...

from(this.users);

or

import { from as observableFrom } from 'rxjs';
...

observableFrom(this.users);

See also migration to rxjs6 guide

  • https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths



回答2:


Without modifying the existing code, still you will be able to run just by installing "rxjs-compat" package.

npm install rxjs-compat --save



回答3:


This is changed from previous rxjs versions to rxjs6. (RxJS v5.x to v6 Update Guide)

Before rxjs 6

import { Observable } from "rxjs";

let numbers = [1, 5, 10];
let source = Observable.from(numbers);

With rxjs 6

import { from, Observable } from "rxjs";

let numbers = [1, 5, 10];
let source = from(numbers);


来源:https://stackoverflow.com/questions/50186371/property-from-does-not-exist-on-type-typeof-observable-angular-6

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