7

如何在效果中访问我的一部分状态?我当前的效果实现(如下所示)是在SEARCH_REUQUEST调度动作时触发的。但是,我想在调用我的搜索服务以启动 HTTP 请求之前访问一个状态以获取一些搜索参数。

@Effect()
SearchRequest$ = this.actions$
  .ofType(fromSearchActions.SearchActionTypes.SEARCH_REQUEST)
  .pipe(
    switchMap((action: fromSearchActions.SearchRequest) => {
      return this.searchService
        .search(action.payload, action.enrichmentId)
        .pipe(
          map(response => {
            console.group("SEARCH effects");
            console.log(response);
            console.groupEnd();
            return new fromSearchActions.SearchRequestSuccess(response);
          }),
          catchError(error =>
            of(new fromSearchActions.SearchRequestFail(error))
          )
        );
    })
  );

显然,我可以在这里利用一个 RxJS 运算符,但我似乎无法理解如何修改我现有的效果实现以合并它。

@Effect()
SearchRequest$ = this.actions$
  .ofType(fromSearchActions.SearchActionTypes.SEARCH_REQUEST)
  .withLatestFrom(this.featureStore.select(fromFeature.getCurrentSearchPageOptions))
  .pipe(
      switchMap((// How should this change? //) => { //Error
        return this.searchService
          .search(action.payload, action.enrichmentId, pageOptions)
          .pipe(
            map(response => {
              console.group("SEARCH effects");
              console.log(response);
              console.groupEnd();
              return new fromSearchActions.SearchRequestSuccess(response);
            }),
            catchError(error =>
              of(new fromSearchActions.SearchRequestFail(error))
            )
          );
      })
    )

我觉得我接近一个解决方案,但我似乎无法在switchMap舞台上将它放在一起。

4

2 回答 2

4

withLatestFrom:将源 Observable 与其他 Observable 组合以创建一个 Observable,其值是根据每个 Observable 的最新值计算得出的。

您将获得包含这两个数据的数组,您可以在https://medium.com/@viesturv/how-to-get-store-state-in-ngrx-effect-fab9e9c8f087中找到指南

你的代码可能是这样的:

@Effect()
    SearchRequest$ = this.actions.pipe(
        ofType(fromSearchActions.SearchActionTypes.SEARCH_REQUEST),
        withLatestFrom(this.featureStore.select(fromFeature.getCurrentSearchPageOptions)),
        switchMap(([action: Action, pageOptions: YourStateOptions]) => { // <-- pageOptions here
                return this.searchService
                    .search(action.payload, action.enrichmentId, pageOptions)
                    .pipe(
                        map(response => {
                            console.group("SEARCH effects");
                            console.log(response);
                            console.groupEnd();
                            return new fromSearchActions.SearchRequestSuccess(response);
                        }),
                        catchError(error =>
                            of(new fromSearchActions.SearchRequestFail(error))
                        )
                    );
            }
        )
    );
于 2020-08-07T14:19:34.227 回答
2

withLatestFrom 绝对是你想要的。您所要做的就是将响应添加到您在 switchMap 中传递的参数。像这样(其中 pageOptions 是您从选择器获得的状态切片的类型):

switchMap(([action, options]: [fromSearchActions.SearchRequest, pageOptions]) => {

然后您可以action像以前一样使用,options这将是您从中得到的this.featureStore.select(fromFeature.getCurrentSearchPageOptions)

于 2018-10-22T20:51:47.127 回答