Property interval does not exist in the type obser

2020-02-05 06:58发布

问题:

ngAfterViewInit(){
     Observable.interval(3000).timeInterval().subscribe()=>{};    
}

Trying to invoke the Observable.interval() method it is throwing a compiler error "Property interval does not exist in the type observable".

Edit

import { Observable } from 'rxjs/Observable';

Note that the import statement is already included

回答1:

For RxJS 6+ the answer given by Tomasz Kula only applies when using the rxjs-compat package, which should only be used when in the process of converting an application from RxJS 5 to RxJS 6.

Within RxJS 6+, use:

import { interval } from 'rxjs';

interval(3000).subscribe(x => /* do something */)

Note that any Observable creation function that previously existed on the Observable type, should now be imported from 'rxjs'.



回答2:

for rxjs 5.5.2+ it is:

import { interval } from 'rxjs/observable/interval';

usage:

interval(3000).subscribe(x => // do something)


回答3:

this is correct for angular 6.1.+ and rxjs 6.2.+

import { Observable } from 'rxjs';
import { interval } from 'rxjs';

 interval(1000).subscribe(
         (value: number) => {
           this.secondes = value;
         },
         (error: any) => {
           console.log('error');
         },
         () => {
           console.log('observable completed !');
         }
       );