Angular 2 with ngrx/store,更新状态对象的 属性 而不覆盖整个对象
Angular 2 with ngrx/store, updating property of state object without overwriting the entire object
我正在尝试分配一个新的 属性 状态对象,但如果不设置一个全新的对象我似乎无法设置它,我只想添加一个 属性,不要只用那个 属性.
创建一个全新的对象
这是我的减速器:
const initialState = {
all: [],
single: {}
}
export const ModelsReducer: ActionReducer<any> = (state: IModelsStorage = initialState, action: Action) => {
switch(action.type) {
case GET_MODELS_SUCCESS: return Object.assign({}, state, {
all: [...action.payload.data]
});
case GET_MODEL_SUCCESS: return Object.assign({}, state, {
single: action.payload.data
});
// How do I only update the single object without overwriting the whole thing?
case GET_VARIANTS_SUCCESS: return Object.assign({}, state, {
single: {
variants: action.payload.data
}
});
default: return state;
}
}
我该怎么做?
与另一个 Object.assign...
case GET_VARIANTS_SUCCESS: return Object.assign({}, state, {
single: Object.assign(state.single, {
variants: action.payload.data
})
});
我正在尝试分配一个新的 属性 状态对象,但如果不设置一个全新的对象我似乎无法设置它,我只想添加一个 属性,不要只用那个 属性.
创建一个全新的对象这是我的减速器:
const initialState = {
all: [],
single: {}
}
export const ModelsReducer: ActionReducer<any> = (state: IModelsStorage = initialState, action: Action) => {
switch(action.type) {
case GET_MODELS_SUCCESS: return Object.assign({}, state, {
all: [...action.payload.data]
});
case GET_MODEL_SUCCESS: return Object.assign({}, state, {
single: action.payload.data
});
// How do I only update the single object without overwriting the whole thing?
case GET_VARIANTS_SUCCESS: return Object.assign({}, state, {
single: {
variants: action.payload.data
}
});
default: return state;
}
}
我该怎么做?
与另一个 Object.assign...
case GET_VARIANTS_SUCCESS: return Object.assign({}, state, {
single: Object.assign(state.single, {
variants: action.payload.data
})
});