Angular rxjs Observable.timer不是带导入的函数

maa*_*aac 8 rxjs typescript ngrx angular

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

ERROR TypeError:rxjs_Observable__WEBPACK_IMPORTED_MODULE_4 __.ObMapable.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)at FilterSubscriber.push ../ node_modules/rxjs/_esm5/internal /在ScannedActionsSubject.push的FilterSubscriber.push ../ node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next(Subscriber.js:54)上的运算符/ filter.js.FilterSubscriber._next(filter.js:38).位于SafeSubscriber._next(store.js:332)的SafeSubscriber.push ../ node_modules/rxjs/_esm5/internal /中的./node_modules/rxjs/_esm5/internal/Subject.js.Subject.next(Subject.js:47) Subscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:195)位于苏的SafeSubscriber.push ../ node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next(Subscriber.js:133)bscriber.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)))
      );
    })
  );
}
Run Code Online (Sandbox Code Playgroud)

我一直在寻找网络和我看起来像最新的角度版本将使用

import 'rxjs/add/observable/timer';
Run Code Online (Sandbox Code Playgroud)

但是,它似乎没有用.有谁知道如何解决这个问题?

Mar*_*mek 12

如果你使用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)))
    );
  })
);
Run Code Online (Sandbox Code Playgroud)

基本上没有猴子补丁Observable了,现在你需要从中导入该timer函数rxjs并使用它.

更多关于此更改的信息如下:

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