加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:42:54 所属栏目:安全 来源:网络整理
导读:我的服务类,在调用Web服务之前,需要从我的状态获取属性(在我的示例中为:dataForUpdate).目前,我这样做: constructor ( public _store: StoreAppState,public _APIService:APIService) { const store$= this._store.select ('StateReducer'); .../... let u
我的服务类,在调用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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读