繁体   English   中英

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

[英]Angular rxjs Observable.timer is not a function with import

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

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)))
      );
    })
  );
}

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

import 'rxjs/add/observable/timer';

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

如果你使用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猴子补丁,现在你需要从rxjs导入那个timer函数并使用它。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM