Angular 5 ngrx效果没有导出成员'ofType'

时间:2018-01-21 14:50:01

标签: angular typescript rxjs ngrx ngrx-effects

我正在尝试为我的ngrx状态管理器实现Effects。目前我正在使用Angular v5.2.1ngrx v4.1.1rxjs 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'.有任何想法吗?

2 个答案:

答案 0 :(得分:7)

查看@ngrx/effects API,没有迹象表明此库已实现了ofType的可调版本,因此您的第二个实现将无效(至少不会在管道内部使用ofType )。

您的第一个实现只是缺少mergeMap

的导入
import 'rxjs/add/observable/mergeMap';

可能还有mapcatch

import 'rxjs/add/observable/map';
import 'rxjs/add/observable/catch';

如果您想将ofTypepipe一起使用,则可能会有效

@Effect()
getWifiData: Observable<Action> = 
  this.actions$.ofType(WifiTypes.getWifiNetworks)
    .pipe(
      mergeMap((action: GetWifiNetworks) =>
      ...

因为ofType()会返回一个已经添加.pipe的Observable的原型。

脚注

在查看github上的源代码后(截至2018年1月22日),我在platform/modules/effects/src/index.ts找到了一个lettable ofType的导出。

但是在使用@ngrx/effects@latest(它给我的版本4.1.1)安装时,我无法在已安装的node_modules文件夹下看到此导出引用。

在我的组件中,我也不能使用import { ofType } from '@ngrx/effects';

答案 1 :(得分:2)

看起来文档反映了每晚的发布。

https://github.com/ngrx/platform/issues/727

每晚发布可在此处获取:https://github.com/ngrx/effects-builds