1

createSelector当我使用在文件 A 中定义的文件在文件 B 中执行时,我遇到了几次以下错误Selector......显然文件 A 中的Selector定义没有被实例化,我得到一个undefined参考错误......

Error: Selector creators expect all input-selectors to be functions, instead received the following types: [function, undefined] at getDependencies
(http://localhost:3000/index.js:47281:11) [<root>] at Object.createSelector
(http://localhost:3000/index.js:47299:24) [<root>] at Object.<anonymous>
(http://localhost:3000/index.js:170074:45) [<root>] at __webpack_require__
(http://localhost:3000/index.js:20:30) [<root>] at Object.<anonymous>
(http://localhost:3000/index.js:112827:10) [<root>] at __webpack_require__
(http://localhost:3000/index.js:20:30) [<root>] at Object.<anonymous>
(http://localhost:3000/index.js:112838:20) [<root>] at __webpack_require__
(http://localhost:3000/index.js:20:30) [<root>] at Object.<anonymous>
(http://localhost:3000/index.js:85408:17) [<root>] at __webpack_require__
(http://localhost:3000/index.js:20:30) [<root>] at Object.<anonymous>
(http://localhost:3000/index.js:113264:18) [<root>] at __webpack_require__
(http://localhost:3000/index.js:20:30) [<root>] at Object.<anonymous>
(http://localhost:3000/index.js:28456:10) [<root>] at __webpack_require__
(http://localhost:3000/index.js:20:30) [<root>]

由于我遇到过几次这种情况,我的想法是其他人也会遇到类似的问题,但是搜索 stackoverflow 和互联网并没有产生任何有意义的东西......这让我相信我还有另一个根本的误解...... .

归档 A 状态(商店/帐户)

export interface State {
  accounts?: IDenormalizedAccount[];
  id?: AccountID;
}

文件 A 选择器(商店/帐户)

import * as reportStore from 'Store/report';

const rootExtractor: (applicationState: ApplicationStates) => accountStore.State = (applicationState: ApplicationStates) => {
  if (!applicationState.account) {
    throw new Error('Account state was null or undefined within the application ngrx store');
  }

  return applicationState.account;
};

export const accountsExtractor: (state: accountStore.State) => IDenormalizedAccount[] = (state: accountStore.State) => !!state.accounts ? state.accounts : [];
export const accountsSelector = createSelector(rootExtractor, accountsExtractor);

export const reportAccountsExtractor: (accounts: IDenormalizedAccount[], id: ReportID) => IDenormalizedAccount[] = (accounts: IDenormalizedAccount[], id: ReportID): IDenormalizedAccount[] => !!id ? accounts.filter((account: IDenormalizedAccount) => id === account.reportId) : [];
export const reportAccountsSelector: Selector<any, IDenormalizedAccount[]> = createSelector(accountsSelector, reportStore.reportIdSelector, reportAccountsExtractor);

文件 B 状态(存储/报告)

export interface State {
  reports: Report[];
  id?: ReportID;
}

文件 B 选择器(存储/报告)

const rootExtractor: (applicationState: ApplicationStates) => reportStore.State = (applicationState: ApplicationStates) => {
  if (!applicationState.report) {
    throw new Error('Report state was null or undefined within the application ngrx store');
  }

  return applicationState.report;
};

export const reportIdExtractor: (state: reportStore.State) => ReportID = (state: reportStore.State): ReportID => !!state.id ? state.id : undefined;
export const reportIdSelector: Selector<any, ReportID> = createSelector(rootExtractor, reportIdExtractor);

reportStore.reportIdSelector(在显示的最后一行)文件 A 被标记为undefined用于createSelector实例化reportAccountsSelector


过去,我在商店中的“根”对象之间移动或复制了状态片段,但这不是 DRY --- 那么,为什么我的方法失败了,是否有更好(正确)的方法来处理情况我想从应用商店的其他部分访问状态?

4

0 回答 0