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

前端 未结 4 567
轻奢々
轻奢々 2021-02-07 11:40

The code is below

import {Component} from \'angular2/core\';
import {Observable} from \'rxjs/Rx\';

@Component({
selector: \'my-app\',
template: \'Ticks (every s         


        
相关标签:
4条回答
  • 2021-02-07 12:02

    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.

    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

    0 讨论(0)
  • 2021-02-07 12:06

    all you need to do is to import Observable from the root folder of library because older versions of rxjs does not provide complete Observable class in rxjs/Observable

    import {Observable} from 'rxjs';
    
    0 讨论(0)
  • 2021-02-07 12:09

    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)
    
    0 讨论(0)
  • 2021-02-07 12:17

    Thats because you havent patched the timer method into the Observable prototype.

    Update: 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:

    1) Patch the method with:

    import 'rxjs/add/observable/timer';
    

    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.

    0 讨论(0)
提交回复
热议问题