服务不在 Angular 辅助入口点的“rootDir”下

Jir*_*vec 5 angular angular-library ng-packagr

编译 Angular 库时,我收到有关子库的 rootDir 的错误

library/services/src/public-api.ts:31:15 - error TS6059: File 'C:/libname/library/services/src/orders/orders.service.ts' is not under 'rootDir' 'C:/libname\library\classes\src'. 'rootDir' is expected to contain all source files.

我逐渐认识到,在 Angular 库内部的各个辅助条目之间使用相对导入并不是一个好的做法。所以我将我的代码划分为 TSConfig 中的辅助条目 + 设置路径

代码结构

library
  services
    src
      public-api.ts
    package.json
  models
    src
      public-api.ts
    package.json
  src
    public-api.ts
  package.json
Run Code Online (Sandbox Code Playgroud)

TS配置

{
  "rootDir": "./library/",
  "paths": {
    "@core/services": [ "library/services/src/public-api.ts" ]
  }
}
Run Code Online (Sandbox Code Playgroud)

角度.json

{
  "projects": {
    "lib": {
      "root": "library",
      "sourceRoot": "library"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

所以问题是 - 如何修复各个辅助入口点或文件结构之间的导入并使项目编译正常?我知道辅助入口点的编译是单独的,并且“被视为一个单独的项目”,因此出现错误。因此,我是否应该添加单独的辅助入口点作为对等依赖项?

当然还有更多路径和辅助端点 - 但这与问题无关,这足以显示我的文件结构和设置

Vic*_*ias 2

您可以使用相对导入,这会产生辅助入口点根目录之外的文件,例如。import Users from "../../models/src/public_api.ts"

相反,使用库的名称导入模块,例如。import Users from "library/models"

ng-packagr repo 中的这个问题提供了很好的代码示例。