angular - 使用 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))))
));
解决方案
你可以这样做:
@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
推荐阅读
- java - 递归地最小化塔之间的最大差异
- listview - 根据 xamarin 中的 api 响应在列表视图中填充数据
- javascript - window.removeEventListener 在 componentWillUnmount 上不起作用
- python - 在 Pandas、int 和 str 中创建新列时遇到问题
- duktape - 如何在 Duktape 2.5.0 中使用超时来防止 JavaScript 循环等
- r - 如何使用 effect() 函数提取多个变量的边际均值?
- php - 使用 PHP 访问特定的 JSON 对象值
- java - Kafka生产者未来超时
- javascript - 仅在当前元素上更改类(jQuery+this)
- java - 为什么 Eclipse 中的 Maven 更新后 JRE 库发生了变化?