Reducer,实体适配器:属性'accounts'在类型上不存在 - Angular 7,Rxjs 6,Ngrx

问题描述 投票:0回答:1

我收到错误:

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;
    }
  }
}

但是当我查看reducer的相关操作时,他们传递的有效负载是一系列帐户,其名称为“accounts”

export class AllAccountsLoaded implements Action {
  readonly type = AccountActionTypes.AllAccountsLoaded;

  constructor(public payload: {accounts: Account[]}) {
  }
}

因此,有效载荷似乎应该正确传递。令我担心的是错误的一部分:'{account:Account; } | {accounts: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}))
  );
angular rxjs angular7 ngrx reducers
1个回答
0
投票

好。如果其他人可能碰到这个。错误消息非常误导。如果你看一下reducer的定义:

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});

这只是一个错字,但由于错误指向你完全不同的方向,可能需要一段时间才能追踪。

© www.soinside.com 2019 - 2024. All rights reserved.