![](/img/trans.png)
[英]Angular: How to debounce a function call with Observable.timer() and switchMap?
[英]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.