我正在尝试在我使用 @ngrx/store 的 Angular 应用程序中实现撤消操作。我想要做的是分派(dispatch) UNDO 操作,然后一旦操作被处理,数据就被 reducer 更新并通过存储推送,我想用更新的数据做一些事情。
这就是我目前正在尝试实现这一目标的方法。基本上,因为records$来自BehaviourSubject,所以我最初会在订阅时获得一个值。然后我调度该操作并等待更新完成。一定有更好的方法,对吧?
undoRecords() {
let count = 1;
// The records$ observable comes from the ngrx store
let obs = this.records$.take(2).subscribe(records => {
if(count == 1) {
this.store.dispatch({type: UNDO_RECORDS, payload: undefined}); // this will change the records
count++;
}
else {
this.someService.doSomething(records);
}
});
}
最佳答案
我通常做的是三个 Action : Action 、 Action 成功、 Action 错误。
ACTION 会触发实际操作,如果您有兴趣跟踪进度,还会更改商店中的状态 (inprogress=true)。
ACTION_SUCCESS ,将状态“进行中”设置为 false。并更新实际数据。
您可以监控进行中的状态,并且您将知道任务何时完成。
关于javascript - ngrx - 一旦分派(dispatch)的操作成功更新商店,如何执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46476651/