首页 > 解决方案 > 使用 ngrx 从效果访问存储

问题描述

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

我读过一个名为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))))
    ));

标签: angularngrxngrx-effects

解决方案


你可以这样做:


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

有关详细信息,请参阅为此开始使用 ngrx/effects


推荐阅读