如果请求仍待处理,则Ngrx分派操作不起作用

时间:2018-08-12 18:33:58

标签: angular ngrx

我有一个通过订阅获取对象详细信息的组件(目前不涉及ngrx或api)。

获取信息后,我将调度一个动作并检索数据。

很好的期望是当有许多呼叫时,如果请求仍待处理,则不会发生调度动作。

有什么办法可以取消请求并引用上一个电话吗?

还有其他方法或解决方案吗?

this.service.currentMessage.subscribe((selected) => {
      if(selected) {
        this.obj = {
          id: selected.id
        };
        // when there are many calls and a call is still in pending then
        // this row actually not happened and no call made
        this.store.dispatch(new FetchList(this.obj));
      }
    });

谢谢

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是停止多个请求。因此,在您获得数据的服务中,添加RxJs ShareReplay运算符,如下所示:

this.http.get('your url')
.pipe(
shareReplay);

这意味着当您多次请求时,如果请求已经挂起,其他请求将被忽略。