'Error' message: 'Property 'from' does not exist on type 'typeof Observable'

柔情痞子 提交于 2019-12-21 08:50:12

问题


I am trying to learn reactive programming using RxJS. I was trying to create an observable from an array using Observable.from() method, but I am getting an error:

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

I scaffolded an Angular application using Angular CLI, so all the dependencies including RxJS package was imported correctly.

In app.component.ts I added below import statements:

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

And my AppComponent class looks like this:

export class AppComponent {

  numbers: number[] = [1, 2, 3, 4, 5];

  callMyObservable() : void {  
    Observable.from(this.numbers);
  }
}  

But I am getting the above mentioned compile time error.

I am not sure how to make it work.


回答1:


Change

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

To

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



回答2:


If you are using rxjs >=6.0.0 then you no longer use Observable.from. Instead from is a standalone function.

import { Observable, from} from 'rxjs';

//old way
var o = Observable.from([1, 2, 3, 4]);

//new way
var o = from([1, 2, 3, 4]);

I hope this is helpful since it took me a while to figure this out.



来源:https://stackoverflow.com/questions/47003112/error-message-property-from-does-not-exist-on-type-typeof-observable

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