NgRx在带有模型(类)且内部具有函数的减速器中初始状态的问题

时间:2019-12-22 11:45:59

标签: angular ngrx

我与NgRx一起工作,当我在reducer中处于初始状态时,在使用具有功能的模型时遇到问题。

操作:

import { createAction, props } from '@ngrx/store';
import { User } from '../models/user.model';


export const loadUser = createAction (
  '[AUTH] LOAD_USER',
  props<User>()
);

减速器:

import { User } from '../models/user.model';
import * as UserAction from '../actions/user.actions';
import { Action, createReducer, on } from '@ngrx/store';

export const initialState: User = {
  isAuthenticated: false,
  loading: true,
  id: null,
  deloitteID: null,
  fullName: {
    english: null,
    hebrew: null
  },
  email: null,
  rankHebrew: null,
  rankEnglish: null,
  companies: [],
};

const commentReducer = createReducer(
  initialState,
  on(UserAction.loadUser, (state, { isAuthenticated, loading, id, deloitteID, fullName, email,
     rankEnglish, rankHebrew, companies }) => ({ isAuthenticated, loading
    , id, deloitteID,
     fullName, email, rankEnglish, rankHebrew, companies }))
);

export function reducer(state: User | undefined, action: Action) {
  return commentReducer(state, action);
}

型号:

import { Deserializable } from './deserializable.model';

export class User implements Deserializable {
    id: number;
    deloitteID: number;
    fullName: {
      english: string,
      hebrew: string
    };
    email: string;
    rankHebrew: string;
    rankEnglish: string;
    companies: Array<string>;

    deserialize(input: any) {
        Object.assign(this, input);
        return this;
    }

    getNameInitials(): string {
        return this.fullName.english.split(' ').map(n => n[0]).join('').toUpperCase();
    }
}

Reducer错误:

Type '{ isAuthenticated: false; loading: true; id: null; deloitteID: null; fullName: { english: null; hebrew: null; }; email: null; rankHebrew: null; rankEnglish: null; companies: undefined[]; }' is missing the following properties from type 'User': deserialize, getNameInitialsts(2739)

如何使减速器忽略模型中的功能?

问题是我想要使用用户的所有属性的减速器包括这些功能。

0 个答案:

没有答案