“错误"消息:“typeof Observable"类型上不存在“属性"“from" [英] 'Error' message: 'Property 'from' does not exist on type 'typeof Observable'
问题描述
我正在尝试使用 RxJS 学习响应式编程.我试图使用 Observable.from() 方法从数组创建一个 observable,但出现错误:
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:
'typeof Observable' 类型不存在属性 'from'
Property 'from' does not exist on type 'typeof Observable'
我使用 Angular CLI 搭建了一个 Angular 应用程序,所以包括 RxJS 包在内的所有依赖项都被正确导入.
I scaffolded an Angular application using Angular CLI, so all the dependencies including RxJS package was imported correctly.
在 app.component.ts 中,我添加了以下导入语句:
In app.component.ts I added below import statements:
import { Observable} from 'rxjs/Observable'
import 'rxjs/observable/from'
我的 AppComponent 类如下所示:
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.
推荐答案
更改
import { Observable} from 'rxjs/Observable'
import 'rxjs/observable/from'
到
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';
这篇关于“错误"消息:“typeof Observable"类型上不存在“属性"“from"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!