ngrx 商店选择器在从自定义库导入应用程序时失败

问题描述

我有一个带有 store 实现的 angular 库,这个库被打包为 NPM 并在不同的 angular 应用程序中使用。

我正在尝试使用一个 ngrx 存储选择器,该选择器在我的不同 Angular 项目的库中导出,应用程序抛出错误

未找到模块:错误:无法解析 '@lib/core-lib/lib/core-store/store/account/account.selector' 中 'C:\workspace\Client\Angularapp\src\app\app.component'

Angular 库实现:

import { createSelector } from '@ngrx/store';
import { ILibState } from '../../lib.state';
import { IAccountState } from './account.state';

const selectAccounts = (state: IAppState) => state.accounts;

export const selectSelectedAccount = createSelector(
  selectAccounts,(state: IAccountState) => state?.selectedAccount
);

从 public_api.ts 导出这个选择器

export * from './lib/core-store/store/account/account.selector'
// Also tried this
// export { selectSelectedAccount } from './lib/core-store/store/account/account.selector'
// Also tried the barrel approach
// export * from './lib/core-store/store/account/index'

Angularapp 使用

app.component.ts

import { ILibState } from '@lib/core-lib/lib/core-store/lib.state';
import { select,Store } from '@ngrx/store';
import { takeuntil } from 'rxjs/operators';
import { selectSelectedAccount } from '@lib/core-lib/lib/core-store/store/account/account.selector';

this._store
  .pipe(select(selectSelectedAccount),takeuntil(this.destroy$))
  .subscribe((res) => {
    if (res && this.selectedAccountCode !== res) {
      this.selectedAccountCode = res.account;
    }
  });

Angular 库运行良好,没有任何问题,将这个选择器放入我的 Angular 应用程序会导致我在编译时出错。

我尝试提供尽可能多的细节,如果我需要添加任何其他内容,请告诉我。

非常感谢您对此的任何回答

解决方法

如果没有您的文件夹结构和配置的一些背景知识,这将很难,但让我们坚持错误

未找到模块:错误:无法解析“C:\workspace\Client\AngularApp\src\app\”中的“@lib/core-lib/lib/core-store/store/account/account.selector” app.component'

这意味着它找不到您要导入的文件@lib/core-lib/lib/core-store/store/account/account.selector.ts

建议您先检查路径是否正确。

除此之外,我认为您应该仔细检查以下几点:

  • 如果您说这个库在 npm 包中,为什么还要使用别名 @lib?
  • 如果您从 public_api.ts 导出这些功能,为什么还要进行深度导入?