web-dev-qa-db-ja.com

Angular rxjs Observable.timerはインポート付きの関数ではありません

私のangularアプリケーションでは、次のエラーを受け取ります。

エラー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.Switchscribe.next.Map.next.Map.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/FilterSubscriber.Push ../ node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next(Subscriber.js:54)のoperator/filter.js.FilterSubscriber._next(filter.js:38)はScannedActionsSubject.Pushで./node_modules/rxjs/_esm5/internal/Subject.js.Subject.next(Subject.js:47)at SafeSubscriber._next(store.js:332)at SafeSubscriber.Push ../ node_modules/rxjs/_esm5/internal/SuのSafeSubscriber.Push ../ node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next(Subscriber.js:133)のSubscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:195) 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)))
      );
    })
  );
}

私はウェブを見て回っていて、最新のangularバージョンが使用するように見えます

import 'rxjs/add/observable/timer';

ただし、機能していないようです。誰もこの問題を解決する方法を知っていますか?

8
maac

RxJS 6で最新のangularを使用する場合は、次のようにする必要があります。

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

12
Martin Adámek