import { Blog } from './app.model';
import { ActionReducer, Action } from '@ngrx/store';
import * as _ from 'underscore';
export const LOAD_BLOGS = 'LOAD_BLOGS';
export const SAVE_COMMENT = 'SAVE_COMMENT';
export interface AppState {
blogs : Blog[]
}
export const initialState : AppState = {
blogs : []
};
var a;
export const reducer = (state : AppState = initialState, action :Action) => {
switch (action.type) {
case LOAD_BLOGS:
return Object.assign({}, state, {
blogs : action.payload
});
case SAVE_COMMENT:
const {title, comment} = action.payload;
return state.blogs.map(states =>{
if(states.title === action.payload.title){
console.log(states);
return Object.assign({}, states ,{ comment : [...states.comment , action.payload.comment]});
}
return Object.assign({}, states);
});
default:
return state;
}
}
STORE VOR SAVE_COMMENT ACTIONObject.assign() mit komplexem Objekt Angular2 NGRX/store
{blogs : [{id: 1, "title" : "one", "comment" : ["oneC", "twoC"]}, {id: 2, "title" : "two", "comment" : ["oneC", "TwoC"]}]}
STORE NACH SAVE_COMMENT ACTION MIT KOMMENTAR "ThreeC" mit dem Titel bloggen "zwei"
outter Blogs Objekt, das Array von Objekten enthalten sollte, ist nicht da, stattdessen gibt es nur ein Array o f Objekt mit aktualisierten Blogs.
Wenn Sie über 'Object.assign()' wissen, was haben Sie dann ausprobiert? – Jai
Wenn es dringend ist, stellen Sie bitte die Frage so klar wie möglich, anstatt einen Text hinzuzufügen, der dringend ist. –
Ich habe die Frage aktualisiert, bitte schau .. –