【问题标题】:Cannot compile minimal app using npm link to reach external library无法使用 npm 链接编译最小应用程序以访问外部库
【发布时间】:2021-05-20 23:37:45
【问题描述】:

我尝试设置一个简单的组件库以供应用程序使用,并使用 npm link 连接它们,但它甚至无法编译,我不确定我遗漏了什么。它是最小的,只是为了证明该方法有效。

我使用

创建了一个库
  1. ng new lib-test1 --create-application=false
  2. cd lib-test1
  3. ng 生成库 TestLibrary1
  4. ng 构建

然后我创建应用程序来测试它:

  1. ng new app-test1
  2. cd app-test1
  3. ng 构建

问题似乎出在我使用 npm 链接时

  1. cd 到 lib-test1/dist/test-library1> npm 链接
  2. cd to app-test1> npm link test-library1

添加导入:

  1. 在 app-test1/app.component.ts 添加“import {TestLibrary1Component} from 'test-library1';”
  2. 在 app-test1/app.module.ts 中添加 import {TestLibrary1Component} from 'test-library1';
  3. 在 app-test1/app.module.ts 中将 TestLibrary1Component 添加到导入部分
  4. 在app-test1/app.component.html中添加一个标签来引用

ng 构建失败并显示以下内容:

√ 浏览器应用程序包生成完成。

错误:node_modules/test-library1/lib/test-library1.component.d.ts:3:22 - 错误 NG6002:出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule 类。

这可能意味着声明 TestLibrary1Component 的库 (test-library1) 没有被 ngcc 正确处理,或者与 Angular Ivy 不兼容。检查是否有更新版本的库可用,如果有则更新。还可以考虑与图书馆的作者核实,看看图书馆是否应该与 Ivy 兼容。

3 导出声明类 TestLibrary1Component 实现 OnInit { ~~~~~~~~~~~~~~~~~~~~~

链接似乎无法正常运行。 我在 compilerOptions 和 angularCompilerOptions 中都尝试了 "preserveSymLinks" : true 在 tsconfig.json 中,但它没有帮助。 我在这个问题上找到的几乎所有搜索结果都是它成功编译但在运行时出现问题的地方。

我还根据https://dev.to/dontry/using-npm-link-in-angular9-11ie 尝试了一个常春藤友好的构建,但它也没有效果

【问题讨论】:

    标签: angular npm


    【解决方案1】:

    看来问题出在我的导入中,我导入的是 TestLibrary1Component 而不是 TestLibrary1Module

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多