角 – 如何使用@ ngrx / store获取状态对象的当前值?

前端之家收集整理的这篇文章主要介绍了角 – 如何使用@ ngrx / store获取状态对象的当前值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的服务类,在调用Web服务之前,需要从我的状态获取属性(在我的示例中为:dataForUpdate).目前,我这样做:
constructor ( public _store: Store<AppState>,public _APIService:APIService) {

    const store$=  this._store.select ('StateReducer');

    .../...

    let update = this.actions$.filter ( action => action.type==UPDATE )
                              .do( (action) => this._store.dispatch({type: REDUCER_UPDATING,payload : action.payload  }) )
      *** GET STATE ***==>    .mergeMap ( action => store$.map ( (state : AppState)=> state.dataForUpdate ).distinctUntilChanged(),(action,dataForUpdate) { 
                                                   return { type:action.type,payload : {employee:action.payload,dataForUpdate :dataForUpdate }    }; 
                                                            })
       * AND CALL API *==>    .mergeMap ( action =>this._APIService.updateEmployee(action.payload.employee,action.payload.dataForUpdate),APIResult) => { return  { type:REDUCER_UPDATED }})
                              .share ();


    .../...

    let all = Observable.merge (update,....);
    all.subscribe ((action:Action) => this._store.dispatch (action));

}

PS:我使用angular2-store-example(https://github.com/ngrx/angular2-store-example/blob/master/src/app/users/models/users.ts)作为指导;)

我想知道一个更好(更清洁)的方式是否存在?可能是我过渡到“功能编程思维”还不完整…;)

实例:https://plnkr.co/edit/WRPfMzPolQsYNGzBUS1g?p=preview

@ ngrx / store v1.x的原始答案

@ ngrx / store extends BehaviorSubject,它具有可以使用的value属性.

this._store.value

这将是你的应用程序的当前状态,从那里你可以选择属性,过滤器,地图等…

更新:

给我一些时间来计算你的例子是什么(:要获取dataForUpdate的当前值,可以使用:

let x = this._store.value.StateReducer.dataForUpdate;
console.log(x); // => { key: "123" }

@ ngrx / store v2.x的更新

随着版本2的更新,值被删除,如docs所述:

The APIs for synchronously pulling the most recent state value out of Store have been removed. Instead,you can always rely on subscribe() running synchronously if you have to get the state value:

function getState(store: Store<State>): State {
    let state: State;

    store.take(1).subscribe(s => state = s);

    return state;
}
原文链接:https://www.f2er.com/angularjs/142880.html

猜你在找的Angularjs相关文章