Angular rxjs Observable.timer不是导入功能

时间:2018-09-15 08:25:57

标签: angular typescript rxjs ngrx

在我的有角度的应用程序中,我收到以下错误:

  

错误TypeError:   rxjs_Observable__WEBPACK_IMPORTED_MODULE_4 __。Observable.timer不是   功能       在SwitchMapSubscriber.project(hybrid.effect.ts:20)       在SwitchMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / switchMap.js.SwitchMapSubscriber._next   (switchMap.js:34)       在SwitchMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next中   (Subscriber.js:54)       在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / filter.js.FilterSubscriber._next下   (filter.js:38)       在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next中   (Subscriber.js:54)       在ScannedActionsSubject.push ../ node_modules / rxjs / _esm5 / internal / Subject.js.Subject.next   (Subject.js:47)       在SafeSubscriber._next(store.js:332)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:195)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber.next   (Subscriber.js:133)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._next   (Subscriber.js:77)

我的代码如下:

import { Injectable } from '@angular/core';
import { Effect, Actions } from '@ngrx/effects';
import { of } from 'rxjs/observable/of';
import { map, catchError, switchMap } from 'rxjs/operators';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/timer';

import * as hybridActions from '../actions/hybrid.action';
import { FleetStatusReportService } from '../../fleet-status-report.service';

@Injectable()
export class HybridEffects {
  constructor(
    private actions$: Actions,
    private fleetstatusreportService: FleetStatusReportService
  ) {}

  @Effect()
  loadHybrid$ = this.actions$.ofType(hybridActions.LOAD_HYBRID).pipe(
    switchMap(() => Observable.timer(0, 900000)),
    switchMap(() => {
      return this.fleetstatusreportService.getHybridPerformance().pipe(
        map(hybrid => new hybridActions.LoadHybridSuccess(hybrid)),
        catchError(error => of(new hybridActions.LoadHybridFail(error)))
      );
    })
  );
}

我一直在网上浏览,对我来说,最新的角度版本将使用

import 'rxjs/add/observable/timer';

但是,它似乎不起作用。有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:11)

如果您将最新的angular与RxJS 6一起使用,则需要这样做:

import { map, catchError, switchMap } from 'rxjs/operators';
import { Observable, of, timer } from 'rxjs';

loadHybrid$ = this.actions$.ofType(hybridActions.LOAD_HYBRID).pipe(
  switchMap(() => timer(0, 900000)),
  switchMap(() => {
    return this.fleetstatusreportService.getHybridPerformance().pipe(
      map(hybrid => new hybridActions.LoadHybridSuccess(hybrid)),
      catchError(error => of(new hybridActions.LoadHybridFail(error)))
    );
  })
);

基本上不再有Observable的猴子补丁了,现在您需要从timer导入该rxjs函数,并使用它。

有关此更改的更多信息在这里:

https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md