Ngrx Store, Effects, Http Ajax Polling Setup on Angular 2

别说谁变了你拦得住时间么 提交于 2019-12-01 06:08:10

As stated in the other answer, interval is a static function, so it does not exist on the Observable prototype - which is why your error is effected.

Instead, you should be able to achieve what you want using timer.

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';

@Effect()
onLoadTasksLoadTasks$: Observable<Action> = this.actions$
  .ofType(tasksActions.ActionTypes.LOAD_TASKS)
  .switchMap(() => Observable
    .timer(0, 10000)
    .switchMap(() => this.TS.index()
      .map((res) => new tasksActions.LoadTasksSuccessAction(res.json()))
      .catch(err => Observable.of(new tasksActions.LoadTasksFailAction(err)))
    )
  );

With timer, it's possible to specify an initial delay and here it's set to zero so that the timer fires immediately. After that, it will fire every ten seconds, but if another LOAD_TASKS action is received, the switchMap will see it unsubscribed and a new timer created, etc.

In rxjs 5 interval() is a static function. You can only use it as a creator Observable.interval(1).

Try this.

this.actions$.ofType(tasksActions.ActionTypes.LOAD_TASKS)
  .skipUntil(Observable.interval(10000))
  .switchMap(res => {...})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!