【发布时间】:2018-06-30 06:52:14
【问题描述】:
我正在尝试为我的 ngrx 状态管理器实现效果。目前我正在使用Angular v5.2.1、ngrx v4.1.1 和rxjs v5.5.6。
例如,我尝试了“旧”方法
@Effect() login$: Observable<Action> = this.actions$.ofType('LOGIN')
.mergeMap(action =>
this.http.post('/auth', action.payload)
// If successful, dispatch success action with result
.map(data => ({ type: 'LOGIN_SUCCESS', payload: data }))
// If request fails, dispatch failed action
.catch(() => of({ type: 'LOGIN_FAILED' }))
);
但我收到了一个错误Property 'mergeMap' does not exist on type 'Actions<Action>'。
所以我使用了新的pipe 方法。问题是当我尝试导入 ofType 运算符时
// ...
import { Action } from '@ngrx/store';
import { Effect, Actions, ofType } from '@ngrx/effects';
import { map, mergeMap, catchError } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
@Injectable()
export class WifiEffects {
@Effect()
getWifiData: Observable<Action> = this.actions$.pipe(
ofType(WifiTypes.getWifiNetworks),
mergeMap((action: GetWifiNetworks) =>
this.mapService.getWifiNetworks().pipe(
map((data: WifiNetworks) => new GetWifiNetworksSucc(data)),
catchError(() => of(new GetWifiNetworksErr()))
)),
);
constructor (
private actions$: Actions,
private mapService: GoogleMapDataService
) {}
}
我收到一个错误Module '".../node_modules/@ngrx/effects/effects"' has no exported member 'ofType'. 有什么想法吗?
【问题讨论】:
-
你有
npm install @ngrx/effects --save吗?如果您已经拥有它,请尝试仅导入效果模块:import { EffectsModule } from '@ngrx/effects'; -
我在同一条船上 TomP - 尝试学习 ngrx 并不断遇到障碍。示例显示“ofType”应该从“@ngrx/effects”导出,但我也没有看到它。
-
@Z.Bagley ofci 我将模块导入到 app.module.ts ....
标签: angular typescript rxjs ngrx ngrx-effects