使用ngrx从效果访问存储

时间:2019-02-07 12:51:29

标签: angular ngrx ngrx-effects

我需要从效果中访问商店状态,以便使用服务并过滤商店中的属性。

我已经读过一个叫做 withLatestFrom 的运算符,但是使用它没有成功。

如何通过filterTransports效果访问商店?

constructor(private store$: Store<fromFundamentalData.State>) {}


@Effect() filterTransports$ = this.actions$.pipe(
    ofType(fundamentalDataActions.FundamentalDataTypes.FilterTransports),
    mergeMap((action: fundamentalDataActions.FilterTransports) => this.filterTransportsService.filter(action.payload, action).pipe(
        map((transports: any) => (new fundamentalDataActions.FilterSuccess(transports))))
    ));

1 个答案:

答案 0 :(得分:1)

您可以这样做:

@Effect()
shipOrder = this.actions.pipe(
  ofType<ShipOrder>(ActionTypes.ShipOrder),
  map(action => action.payload),
  withLatestFrom(this.store.pipe(select(getUserName))),
  map([payload, username] => {
    ...
  })
)

有关更多信息,请参见Start using ngrx/effects for this