我正在创建一个人员减速器...
但是,我们知道请求很可能会失败,这就是为什么我插入了getInitialState()
一些属性,例如data
,error
和loading
.
这data
是一种能够输入特定人的值的object
类型。People
如果请求中发生错误,则它error
是一个object
类型。RequestError
最后loading
是请求的加载。
出现的错误是说RequestError
接口中没有签名People
接口,这很明显,因为我是使用状态作为基础的。
我该如何做这个实现?
错误:
var error: RequestError
Argument of type 'RequestError' is not assignable to parameter of type 'People'.
Type 'RequestError' is missing the following properties from type 'People': id, isMain, avatar, messagests(2345)
行动:
import { createAction, props } from '@ngrx/store';
import { RequestError } from '@shared/interfaces/request.interface';
import { People } from '@shared/interfaces/people.interface';
export const GET = createAction('[GET: USER/API] Get a people');
export const CREATE = createAction('[POST: USER/API] Create a new people', props<{ data: People }>());
export const CREATE_SUCCESS = createAction('[POST: USER/API] Successfully created person', props<{ data: People }>());
export const CREATE_FAIL = createAction('[POST: USER/API] Failed to create a new person', props<{ error: RequestError }>());
export const UPDATE = createAction('[PUT: USER/API] Update a people', props<{ id: string; changes: Partial<People> }>());
export const UPDATE_SUCCESS = createAction('[PUT: USER/API] Person updated with success', props<{ id: string; changes: Partial<People> }>());
export const UPDATE_FAIL = createAction('[PUT: USER/API] Person update failed', props<{ error: RequestError }>());
减速器:
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Action, createReducer, on } from '@ngrx/store';
import { People } from '@shared/interfaces/people.interface';
import * as fromPeopleAction from './people.actions';
export interface State extends EntityState<People> {}
export const adapter: EntityAdapter<People> = createEntityAdapter<People>({
selectId: (people: People) => people.id,
});
const INIT_STATE: State = adapter.getInitialState({
data: null,
error: null,
loading: false,
});
const peopleReducer = createReducer(
INIT_STATE,
on(fromPeopleAction.CREATE, (state: State, { data }) => adapter.addOne(data, { ...state, loading: true })),
on(fromPeopleAction.CREATE_SUCCESS, (state: State, { data }) => adapter.addOne(data, { ...state, loading: false })),
on(fromPeopleAction.CREATE_FAIL, (state: State, { error }) => adapter.addOne(error, { ...state, loading: false }))
);
export function reducer(state: State | undefined, action: Action) {
return peopleReducer(state, action);
}