ngrx store reducer wrapping return value in object

2019-08-18 15:23发布

This is my second question related to ngrx/store today :(

I am using ngrx/store in my Angular 5 project. The Application state I store looks something like this (for illustrative purpose only)

class AppState{
    private customerList: Customer [];
    private selectedCustomer: Customer;
    private countriesOperational: Country [];
}

I have created a single reducer for the state object, as follows:

export function CustomerReducer(state: AppState = new AppState(), action: Action
    ){
        switch(action.type){
            case 'updateSelectedCustomer':
                   return {...state, selectedCustomer:action.payload} 
             //other cases here
            default:
                   return state;
        }
    }

My module has the following:

StoreModule.forRoot({applicationState:CustomerReducer});

In my component, when I do a store.select(state => state), the object I get is wrapped in an internal object applicationState (from the forRoot).

So I am unable to use strongly typed values, since applicationState is not a part of AppState; it is only being returned by the reducer. I have to do something like this:

let state$:Observable<any> = store.select(state => state);
state$.subscribe(newState => this.currentState = newState.applicationState);

this same issue is referenced here: Is there any way to use strongly typed values here instead of any. For e.g. it should be something like:

let state$:Observable<AppState> = store.select(state => state);
state$.subscribe(newState => this.currentState = newState);

0条回答
登录 后发表回答