angular - ngrx 效果中的 switchMap 取消选项 http 请求

标签 angular xmlhttprequest rxjs ngrx-effects

我有一个这样的 ngrx 效果:

@Effect()
  throwError$: Observable<Action> = this.actions$.pipe(
    ofType<notificationActions.ErrorThrow>(
      notificationActions.ActionTypes.ThrowError
    ),
    tap(() => {
      this.store.dispatch(new masterActions.LoadingHide());
      this.sub = this.devTools.liftedState.subscribe(data => {
        this.errorLogService.addErrorLog(JSON.stringify(data)).subscribe(data2 => console.log('data2', data));
        console.log(JSON.stringify(data));
      });
    }),
    map(action => action.payload),
    tap(() => {
      this.sub.unsubscribe();
    }),
    mergeMap(error => {
      return of(
        new notificationActions.ErrorAdd({
          type: error.type,
          description: error.description
        })
      );
    })
  );

errorLogService 的 addErrorLog 方法发送一个 http post 请求,将错误日志存储到数据库中。一切都在执行这样的请求,首先是选项请求,然后是发布请求,但是如果我切换到 switchMap 以组合两个可观察对象,则当我在 chrome 开发工具的网络选项卡上检查它时,选项请求将被取消。

这是重构的代码部分
tap(() => {
      this.store.dispatch(new masterActions.LoadingHide());
      this.sub = this.devTools.liftedState
        .pipe(
          switchMap(data => {
            console.log('data', data);
            return this.errorLogService.addErrorLog(JSON.stringify(data));
          })
        )
        .subscribe();
    })

任何线索?

最佳答案

我遇到了同样的问题。更换 switchMapmergeMap为我做的,现在每个 HTTP 请求都在等待完成。

关于angular - ngrx 效果中的 switchMap 取消选项 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118528/

相关文章:

javascript - 使用 OpenLayers 在 OSM 中显示自定义本地镜像标记

angular - Rxjs 可观察的返回错误时抛出错误 错误被捕获并返回

javascript - 视频标签的 XMLHttpRequest?

javascript - fetch、ajax 和 xhr 之间的区别

linux - 在服务器端使用 bash 代替 PHP/Perl 或 Java

javascript - 在 Observable 上使用 await 时,我应该使用什么来代替 toPromise()?

angular - 在带有 SystemJS 的组件中使用 templateUrl 的相对路径

angular - Ionic 显示等于该值的数据

rxjs - Rxjs Scheduler:asap和async有什么区别?

javascript - 订阅 onComplete 永远不会用 flatMap 完成