Search code examples
angularionic-frameworkngrx

ionic angular ngrx how to access a store


i am working on an ionic angular cordova app. I am trying to print my state, for example, all the objects in the state. However it cannot find the objects i putted inside.

model

export interface Room {
    id: number;
    name: string;
}

action

export class AddRoom implements Action {
    readonly type = ADD_ROOM;

    constructor(public payload: Room){}
}

reducer

const initialState: Room = {
    id: 111,
    name: 'null'
}

export function reducer(state: Room[] = [initialState], action: RoomActions.Actions ){
    switch (action.type) {
        case RoomActions.ADD_ROOM:
            return [...state];
        case RoomActions.REMOVE_ROOM:
            state.splice(action.payload, 1);
            return state;
            default:
                return state;
    }
}

code

rooms2: any;


constructor(private store: Store<AppState>){
      this.rooms2 = this.store.select('room');
      }
console.log(this.rooms2);

but in the console, i can only see a store object in which i cannot find any data i am looking for.


Solution

  • The NgRx Store is reactive.

    this.rooms2 = this.store.select('room'); returns an Observable, and this is probably what you're seeing.

    Try the following:

    this.rooms2.subscribe(rooms => console.log(rooms))