代码之家  ›  专栏  ›  技术社区  ›  Andrew

ngrx-一旦调度的操作成功更新存储,如何执行操作?

  •  1
  • Andrew  · 技术社区  · 8 年前

    @ngrx/store . 我想做的是调度撤销操作,然后一旦操作被处理,数据被reducer更新并被推送到存储中,我想用更新后的数据做些什么。

    以下是我目前努力实现这一目标的方式。基本上,因为records$来自一个BehaviorSubject,我最初会在subscribe上得到一个值。然后我调度动作,等待更新完成。一定有更好的方法,对吗?

    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);
            }
        });
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Vamshi    8 年前

    我通常做三个动作:

    如果您有兴趣跟踪进度,操作会触发实际操作,并更改存储中的状态(inprogress=true)。

    ACTION_SUCCESS,将状态“inprogress”设置为false。并更新实际数据。

    您可以监视inprogress状态,并知道任务何时完成。

        2
  •  0
  •   George Metsis    8 年前

     npm install --save ngrx-undo
    

    在AppModule中:

    import {handleUndo, configureBufferSize} from 'ngrx-undo';
    
    // if you want to update the buffer (which defaults to 100)
    configureBufferSize(150);
    
    @NgModule({
        imports: [
            // pass the handleUndo in the metaReducers
            StoreModule.provideStore(rootReducer, {metaReducers: [handleUndo]}) 
        ]
    })
    export class AppModule { }
    

    要撤消操作,只需使用undo action creator。

    import {undo} from "ngrx-undo";
    
    // create an action
    let action = {type: REMOVE_WINE, payload: {id: wine.id}};
    
    // dispatch it
    this.store.dispatch(action);
    
    // undo the action
    this.store.dispatch(undo(action));
    

    here

    推荐文章