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

imP*_*mPK 5 ngrx angular ngrx-store angular-library ng-packagr

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

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

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

角度库实现:

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
);
Run Code Online (Sandbox Code Playgroud)

从 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'
Run Code Online (Sandbox Code Playgroud)

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;
    }
  });
Run Code Online (Sandbox Code Playgroud)

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

我试图提供尽可能多的细节,并让我知道是否需要添加任何其他内容。

非常感谢对此的任何回答

Ign*_*asi 5

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

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

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

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

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

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