在ngrx效果内一起访问状态和有效载荷数据

时间:2019-02-08 14:20:13

标签: rxjs state ngrx chaining payload

我以ngrx状态保存了一些小部件实例(第三方非角度库),并且需要通过用户操作用新的参数更新小部件。对于这种情况,我以新的小部件参数作为有效负载来调度动作。

是否可以在没有附加私有字段的情况下获得有效载荷和状态数据?

@Effect({ dispatch: false })
public updateStatistics: Observable<webChatActions.Actions> = this._actions.pipe(
    ofType(Types.UPDATE_STATISTICS),
    map((action: demoActions.UpdateStatistics) => action.payload),
    tap((payload: StatisticsOptions) => console.log(payload)),
    withLatestFrom(this._store),
    map(([action, state]): DemoEffects => state['web-chat']),
    tap((state: WebChatState) => {
    // here I have my state, but also I need payload from tap above
    }),
    catchError((error: Error) => {
        this._logger.error('unable to update feedback widget', error);
        return of(new webChatActions.Service.ShowChatError(error));
    })
);

1 个答案:

答案 0 :(得分:1)

删除map,就可以了。

@Effect({ dispatch: false })
public updateStatistics: Observable<webChatActions.Actions> = this._actions.pipe(
    ofType(Types.UPDATE_STATISTICS),
    map((action: demoActions.UpdateStatistics) => action.payload),
    tap((payload: StatisticsOptions) => console.log(payload)),
    withLatestFrom(this._store),
    map((): DemoEffects => state['web-chat']),
    tap(([action, state]) => {
      // action.payload 
      // tate['web-chat']
    }),
    catchError((error: Error) => {
        this._logger.error('unable to update feedback widget', error);
        return of(new webChatActions.Service.ShowChatError(error));
    })
);