简体   繁体   English

错误:在 typeof Observable 上不存在属性计时器

[英]Error: Property timer doesn't exist on type typeof Observable

The code is below代码如下

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

The definition of error is "property timer doesn't exist on type typeof Observable" Why am I getting error like that?错误的定义是“属性计时器在 typeof Observable 上不存在” 为什么我会收到这样的错误? What do you think?你怎么认为?

Thats because you havent patched the timer method into the Observable prototype.那是因为您还没有将timer方法修补到Observable原型中。

Update: Rxjs 6.0.0更新:Rxjs 6.0.0

Import the creation method as a static pure function:将创建方法导入为静态纯函数:

import { timer } from 'rxjs';
let timer$ = timer(2000,1000);

Original answer:原答案:

You have 2 options:您有 2 个选择:

1) Patch the method with: 1)使用以下方法修补方法:

import 'rxjs/add/observable/timer';

2) Import the operator as a static pure function: 2) 将操作符作为静态纯函数导入:

import { timer } from 'rxjs/observable/timer';
let timer$ = timer(2000,1000);

Personally I would recommend the 2nd approach.我个人会推荐第二种方法。

The timer function now need to be imported directly from rxjs library, Below works fine.The message 'fired' will be seen in console after 10 seconds.现在需要直接从rxjs库中导入计时器功能,下面工作正常。10 秒后将在控制台中看到消息“已触发”。

import { timer } from 'rxjs';

const numbers = timer(10000);
numbers.subscribe(any => console.log('fired'));

Please see this link for more details: https://rxjs-dev.firebaseapp.com/api/index/function/timer请参阅此链接了解更多详情: https : //rxjs-dev.firebaseapp.com/api/index/function/timer

您需要做的就是从库的根文件夹中导入Observable,因为旧版本的 rxjs 没有在 rxjs/Observable 中提供完整的 Observable 类

import {Observable} from 'rxjs';

If all you need is a timer, you could use this too:如果您只需要一个计时器,您也可以使用它:

setInterval(() => {
      this.callNecessaryMethod();
    }, this.intervalInMilliSeconds);

This is the function prototype:这是函数原型:

function setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer (+2 overloads)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:类型“ typeof Observable”上不存在属性“ timer”。 &#39;switchMap&#39;和&#39;takeUntil&#39;打破了流程 - error TS2339: Property 'timer' does not exist on type 'typeof Observable'. 'switchMap' and 'takeUntil' breaks the flow &#39;错误&#39;消息:&#39;属性&#39;来自&#39;不存在类型&#39;typeof Observable&#39; - 'Error' message: 'Property 'from' does not exist on type 'typeof Observable' 错误:类型为“可观察”的属性映射不存在 <Response> &#39; - Error: property map doesn't exist on type 'Observable<Response>' “typeof Observable”类型上不存在属性“interval” - Property 'interval' does not exist on type 'typeof Observable' rxjs 6 属性“of”在类型“typeof Observable”上不存在 - rxjs 6 Property 'of' does not exist on type 'typeof Observable' 返回时类型“typeof Observable”上不存在属性“of” - Property 'of' does not exist on type 'typeof Observable' on return 类型&#39;typeof Observable&#39;上不存在属性&#39;subscribe&#39; - Property 'subscribe' does not exist on type 'typeof Observable' Angular 8:属性“of”在类型“typeof Observable”上不存在 - Angular 8: Property 'of' does not exist on type 'typeof Observable' 错误TS2339:类型“ typeof Observable”上不存在属性“ combineLatest” - error TS2339: Property 'combineLatest' does not exist on type 'typeof Observable' RxJS 已更新,“typeof Observable”类型上不存在“merge”属性 - RxJS updated, Property 'merge' does not exist on type 'typeof Observable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM