Search code examples
angulartypescriptngrxngrx-storengrx-store-4.0

How get state in feature module? (ngrx)


I develop the application with angular and ngrx.

How get a state in feature module?

this.todos$ = this.store.select(state => state.todos.data)

I use this tutorial but I cannot understand what is fromStore


Solution

  • if your reducer.ts export that

    export function myList(state = [], action: Action) {
    switch (action.type) {
        case 1:
            return data1;
        case 2:
            return data2;
        default:
            return state;
     }
    }
    

    your store.model.ts must be this:

     export interface AppStore {
         myList: element[];
         ...
     }
    

    and you can get the state like this :

    constructor(private store: Store<AppStore>) {}
    
    this.todos$ = this.store.select("myList")
    

    that will return an Observable of elements array