在redux-observable epic中发送请求之前触发一个动作

时间:2018-03-26 04:29:45

标签: javascript angular rxjs redux-observable angular-redux

我想在向服务器发送请求之前触发操作。这是我的代码:

<div id="startfrom">
    <label for="from">From</label>
    <input type="text" id="from" name="From">
</div>
<div id="endto">
    <label for="to">to</label>
    <input type="text" id="to" name="To">
</div>

我尝试了很多方法,并以此方式结束。但是,这种方式有时会起作用,但有时并不起作用。有时它会冻结整个过程。如何在将请求发送到服务器之前触发操作?

2 个答案:

答案 0 :(得分:2)

您可以从showLoading史诗中删除fetchUserProjects发送内容,然后只需创建一个第二个史诗:

return action$.pipe(
      ofType(ProjectActionType.FETCH_USER_PROJECTS),
      map(() => this._commonAction.showLoading())
    );

执行的顺序并不重要,因为请求this._projectService.getProjectList是异步的,因此肯定会在之后解决。

答案 1 :(得分:0)

这个怎么样:

 return action$.pipe(
      ofType(ProjectActionType.FETCH_USER_PROJECTS),
      flatMap(action => Observable.concat(
        this._projectService.getProjectList(action.payload)
            .map(this._projectAction.addUserProjects({ project: result}))
            .startWith(this._commonAction.showLoading())
            .catch(error => this._commonAction.showError(error)),
        Observable.of(this._commonAction.hideLoading())
      )))