2

I have my observable with interval and takeuntil which is working fine in angular 5. Now When I update it to angular 6 and rxjs6, it is failing. I changed from Observable.interval to interval but takeuntil is not working. I need takeuntil because, I need to stop this observable when the component is destroyed.

import { Observable, Subject } from "rxjs";

private onDestroy$ = new Subject<void>();

Observable.interval(600000).takeUntil(this.onDestroy$).subscribe(x => {
    // do something
});

ngOnDestroy() {
    this.onDestroy$.next();
}
jo_va
  • 13,504
  • 3
  • 23
  • 47
indra257
  • 66
  • 3
  • 24
  • 50

2 Answers2

3

Since RXJS 6, you have to pipe the operators and import them from rxjs/operators. Also, you have to use interval and not Observable.interval, see this doc for more info:

import { interval } from 'rxjs;
import { takeUntil } from 'rxjs/operators';

interval(600000).pipe(
    takeUntil(this.onDestroy$)
).subscribe(x => {
    // do something
});

jo_va
  • 13,504
  • 3
  • 23
  • 47
0

Now it's

Observable.interval(600000).pipe(
   takeUntil(this.onDestroy$)
).subcribe(//...
mbojko
  • 13,503
  • 1
  • 16
  • 26