首页 > 解决方案 > 从本地 Angular 库自动导入包括“public-api”并中断应用程序

问题描述

我有一个 Angular 7 库,我的应用程序将在项目文件夹中使用它。当我从编译的库中自动导入时,Visual Studio Code 在参考中使用公共 api 表面的路径,如下所示:

import { ModuleName } from '@org/lib/public-api';

这导致应用程序中出现编译错误:

ERROR in src/app/app.module.ts(9,28): Error during template compile of 'AppModule'
[2]   Could not resolve @org/lib relative to [object Object]..

当我手动修复导入以删除“public-api”部分时,如下所示:

import { ModuleName } from '@org/lib';

该应用程序重新编译并且很好。我认为我没有更改任何默认配置。这就是我的 tsconfig 在工作区根目录中的样子,而我的库和应用程序 tsconfig 只是随附的开箱即用扩展ng generate

工作区tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "dom"],
    "paths": {
      "@org/lib": ["dist/org-lib"],
      "@org/lib/*": ["dist/org-lib/*"]
    }
  }
}

有没有人遇到过这个?

标签: angulartypescriptangular7angular-library

解决方案


如果以后有人遇到这个问题,可以通过查看如何为本地开发设置 Angular 项目来找到答案。除了他们的 public_api 文件,他们还有一个 index.ts

export * from './public_api';

这使您可以在开发期间在库之间进行交叉引用。


推荐阅读