2017-05-14 28 views
1

我試圖與ngrx商店合作並面臨問題,以正確推送我的商店中的數據並選擇它。如何正確推送和接收來自ngrx商店的數據

我的存儲對象看起來像

AppStore = { 
    chat: { 
    messages: [] 
    } 
} 

和我減速的樣子

const initialState = { 
    messages: [] 
}; 

export const ChatReduce = (state = initialState, action:Action) => { 
    if (typeof state === 'undefined') { 
    return initialState; 
    } 
    switch(action.type){ 
    case 'ADD_MESSAGE': 
     return { 
     ...state, 
     messages: [...state.messages, action.payload] 
     }; 
    default: 
     return state; 
    } 
}; 
萬一

「ADD_MESSSAGE」我想從行動有效載荷推新的消息對象,並返回新的狀態。我究竟做錯了什麼?現在我的狀態聊天消息數組只是每次推送新消息時重寫一個值,但舊消息不是保存。

而寫入存儲後,如何選擇我的狀態消息?是否需要訂閱數據?我試過this.store.select('chat')但是如何獲取消息?

回答

1

繼承人如何我走近它,

ngOnInit() { 
    this.getStudents(); 
    this.students$ = this.store.select(state => state.students) 
    this.students$.forEach(v => console.log(v)) 
    } 

    getStudents(){ 
    // we want to dispatch na action 
    this.store.dispatch(new studentActions.LoadStudentsAction()) 
} 
相關問題