错误:类型为typeof Observable的属性计时器不存在 [英] Error: Property timer doesn't exist on type typeof Observable
本文介绍了错误:类型为typeof Observable的属性计时器不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码在下面
import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'my-app',
template: 'Ticks (every second) : {{ticks}}'
})
export class AppComponent {
ticks =0;
click(){
let timer = Observable.timer(2000,1000);
timer.subscribe(t=>this.ticks = t);
}
}
但是我遇到一个错误.错误在以下行:
But i am getting an error. The error is in the following line:
let timer = Observable.timer(2000,1000);
错误的定义是类型为Obofable的属性计时器不存在" 为什么我会收到这样的错误? 你觉得呢?
The definition of error is "property timer doesn't exist on type typeof Observable" Why am I getting error like that? What do you think?
推荐答案
那是因为您尚未将timer
方法修补到Observable
原型中.
Thats because you havent patched the timer
method into the Observable
prototype.
更新:Rxjs 6.0.0
将创建方法导入为静态纯函数:
Import the creation method as a static pure function:
import { timer } from 'rxjs';
let timer$ = timer(2000,1000);
原始答案:
您有2个选择:
1)使用以下方法修补该方法:
1) Patch the method with:
import 'rxjs/add/observable/timer';
2)将操作符导入为静态纯函数:
2) Import the operator as a static pure function:
import { timer } from 'rxjs/observable/timer';
let timer$ = timer(2000,1000);
我个人会推荐第二种方法.
Personally I would recommend the 2nd approach.
这篇关于错误:类型为typeof Observable的属性计时器不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文