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

Tom*_*omP 8 rxjs typescript ngrx ngrx-effects angular

我正在尝试为我的ngrx状态管理器实现Effects.目前我正在使用Angular v5.2.1,ngrx 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' }))
);
Run Code Online (Sandbox Code Playgroud)

但我收到了一个错误 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
  ) {}

}
Run Code Online (Sandbox Code Playgroud)

我得到一个错误Module '".../node_modules/@ngrx/effects/effects"' has no exported member 'ofType'.任何想法?

Ric*_*sen 7

看看@ngrx/effects API,没有迹象表明这个库已经实现了一个lettable版本ofType,所以你的第二个实现不起作用(至少不在ofType管道内部).

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

import 'rxjs/add/observable/mergeMap';
Run Code Online (Sandbox Code Playgroud)

大概mapcatch以及

import 'rxjs/add/observable/map';
import 'rxjs/add/observable/catch';
Run Code Online (Sandbox Code Playgroud)

如果您想使用ofTypepipe,这可能会工作

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

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


脚注

在浏览了github上的源代码之后(截至2018年1月22日),我在ofType这里找到了一个lettable export / modules/effects/src/index.ts.

但在安装时@ngrx/effects@latest(它给我的版本4.1.1)我在安装的node_modules文件夹下看不到这个导出引用.

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

  • 它不只是在一个示例应用程序中,而是遍布文档 - https://github.com/ngrx/platform/blob/master/docs/effects/README.md (2认同)