【发布时间】:2017-07-19 01:06:23
【问题描述】:
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 BEFORE SAVE_COMMENT ACTION
{blogs : [{id: 1, "title" : "one", "comment" : ["oneC", "twoC"]}, {id: 2, "title" : "two", "comment" : ["oneC", "TwoC"]}]}
STORE AFTER SAVE_COMMENT ACTION WITH COMMENT "ThreeC" 到标题为 "two" 的博客
[{id: 1, "title" : "one", "comment" : ["oneC", "twoC"]},{id: 2, "title" : "two", "comment" : ["oneC", "TwoC", "ThreeC"]}]
应该包含对象数组的外部博客对象不存在,而是只有一个包含更新博客的对象数组。
【问题讨论】:
-
如果您知道
Object.assign(),那么您尝试过什么? -
如果很紧急,请尽可能让问题清楚,而不是添加紧急的文字。
-
我已经更新了问题,请看..
标签: angular ecmascript-6 ngrx