我得到了错误:
ERROR in src/app/account/account.reducers.ts(24,37): error TS2339: Property 'accounts' does not exist on type '{ account: Account; } | { accounts: Account[]; }'.
Property 'accounts' does not exist on type '{ account: Account; }'.
这是指reducer中适配器上的addAll行:
export interface AccountState extends EntityState<Account> {
allAccountsLoaded : boolean;
}
export const adapter : EntityAdapter<Account> = createEntityAdapter<Account>();
export const initialAccountState: AccountState = adapter.getInitialState({
allAccountsLoaded: false
});
export function accountReducer(state = initialAccountState, action: AccountActions): AccountState {
switch(action.type) {
case AccountActionTypes.AccountLoaded:
adapter.addOne(action.payload.account, state);
case AccountActionTypes.AllAccountsLoaded:
adapter.addAll(action.payload.accounts, {...state, allAccountsLoaded: true});
default: {
return state;
}
}
}
但是当我看一下减速器的相关动作时,它们传递的有效载荷是一个以道具名称为“ accounts”的帐户数组。
export class AllAccountsLoaded implements Action {
readonly type = AccountActionTypes.AllAccountsLoaded;
constructor(public payload: {accounts: Account[]}) {
}
}
因此,似乎应该正确传递有效负载。令我担心的是该错误的一部分:'{account:Account; } | {个帐户:帐户[]; }'。我已经看到Ngrx抛出这种错误,如果我在可观察到的效果中错误地键入更改事件名称之一的错误,但我已经对其进行了检查,并且它看起来不错,第一个视图:
@Effect()
loadAllAccounts$ = this.actions$
.pipe(
ofType<AllAccountsRequested>(AccountActionTypes.AllAccountsRequested),
withLatestFrom(this.store.pipe(select(allAccountsLoaded))),
filter(([action, allAccountsLoaded]) => !allAccountsLoaded),
mergeMap(() => this.accountService.getUserAccounts()),
map(accounts => new AllAccountsLoaded({accounts}))
);
答案 0 :(得分:0)
好的。如果有人碰到这个。该错误信息非常容易引起误解。如果您看一看减速器的定义:
export function accountReducer(state = initialAccountState, action: AccountActions): AccountState {
switch(action.type) {
case AccountActionTypes.AccountLoaded:
adapter.addOne(action.payload.account, state);
case AccountActionTypes.AllAccountsLoaded:
adapter.addAll(action.payload.accounts, {...state, allAccountsLoaded: true});
default: {
return state;
}
}
}
您会注意到,在适配器存储库样式函数附近您缺少return子句,因此应该是:
return adapter.addAll(action.payload.accounts, {...state, allAccountsLoaded: true});
这只是一个错字,但由于错误将您指向完全不同的方向,因此可能需要您花一些时间来查找。