我的服務類在調用Web服務之前需要從我的狀態獲取名爲dataForUpdate
的屬性。目前,我正在做這樣的:如何使用@ ngrx/store獲取State對象的當前值?
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),
(action, APIResult) => { return { type:REDUCER_UPDATED }})
.share();
.../...
let all = Observable.merge (update, ....);
all.subscribe ((action:Action) => this._store.dispatch (action));
}
我使用angular2店 - 例如(https://github.com/ngrx/angular2-store-example/blob/master/src/app/users/models/users.ts)爲指導,以跟隨。
我想知道是否存在更好(更清潔)的方式?
活生生的例子:對@ NGRX /存儲1.x版https://plnkr.co/edit/WRPfMzPolQsYNGzBUS1g?p=preview
那麼容易做到。例如:this._store.value.StateReducer.dataForUpdate –
你讓它變得有點複雜了(: – Sasxa
我同意!爲什麼我的感覺是更簡單的解決方案必須存在,謝謝你的幫助 –