Property 'from' does not exist on type 

2020-02-12 07:22发布

问题:

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