【问题标题】:Storing and instance instead of an array in rxjs5 Store在 rxjs5 Store 中存储和实例而不是数组
【发布时间】:2016-12-13 02:54:35
【问题描述】:

我在减速器上有以下尝试

import { ActionReducer, Action, Store } from '@ngrx/store';
import { Observable } from 'rxjs'
import { Name } from './name.model'

export const ADD_NAME = 'ADD_NAME';

export const name: ActionReducer<any> = ( state = new Name(), action: Action ) => {
  switch ( action.type ) {
    case ADD_NAME:
      return [ state, action.payload ];

    default:
      return state;
  }
};

我想在 Store 中存储一个 Name 实例。目前 ADD_NAME 返回一个数组。我怎样才能只存储一个实例而不是数组

【问题讨论】:

    标签: angular angular2-forms rxjs5 reducers


    【解决方案1】:

    你可以用 typescript 2.1+ 写这个:

    case ADD_NAME:
      return { ...state, action.payload };
    

    case ADD_NAME:
      return Object.assign({}, state, action.payload);
    

    【讨论】:

    • 谢谢 Sasxa。两个选项中的第二个是完美的。
    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多