[英]Class is not an Angular module for external library
I have created a simple Angular
app and added a custom library (same project):我创建了一个简单的
Angular
应用程序并添加了一个自定义库(同一个项目):
ng new my-library
ng generate library my-library-lib
ng new my-library
ng generate library my-library-lib
Then to test this simple library in a different project, I built the lib: ng build my-library-lib
然后为了在不同的项目中测试这个简单的库,我构建了这个库:
ng build my-library-lib
I linked the lib in dist folder and linked it to a different project and imported the MyLibraryLibModule
in my SharedModule
我链接了 dist 文件夹中的 lib 并将其链接到不同的项目,并在我的
SharedModule
导入了MyLibraryLibModule
{ MyLibraryLibModule } from 'my-library-lib
imports: [..., MyLibraryLibModule]
--> throws error: class is not an Angular Module
imports: [..., MyLibraryLibModule]
--> 抛出错误: class is not an Angular Module
This is a simple project I did to redo everything from scratch, did not change anything in tsconfig
files etc. Looked online but couldn't find any solution.这是一个简单的项目,我从头开始重做所有内容,没有更改
tsconfig
文件等中的任何内容。在线查看但找不到任何解决方案。
To test it: Library repo: https://github.com/GCour/ui-library测试它:图书馆回购: https : //github.com/GCour/ui-library
Simple project repo: https://github.com/GCour/ui-test简单的项目仓库: https : //github.com/GCour/ui-test
tsconfig.lib.json
: tsconfig.lib.json
:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
tsconfing.lib.prod.json
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {
"enableIvy": false
}
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
],
"paths": {
"my-library-lib": [
"dist/my-library-lib/my-library-lib",
"dist/my-library-lib"
]
}
}
}
If you are asking about use in developer a local library you should create your library in production如果您询问在开发人员中使用本地库,您应该在生产中创建您的库
ng build my-lib --prod
then use npm link
to create locally your library然后使用
npm link
在本地创建您的库
cd dist
cd my-lib
npm link
If all is ok, you can import in another project of your computer, you can check if the lib is added to your local npm in如果一切正常,您可以在您计算机的另一个项目中导入,您可以检查lib是否添加到您本地的npm中
C:\Users\[your-user]\AppData\Roaming\npm\node_modules\my-lib
Now the only thing you need to use in another project of your computer using npm link my-library
现在,您唯一需要使用
npm link my-library
在计算机的另一个项目中使用的东西
cd c:\my-another-app\src
npm link my-library
And use as usually, in your app.module并像往常一样在您的 app.module 中使用
import {myLibModule} from 'my-lib'
//or import {MyLibService} from 'my-lib/public-api'
...
imports: [myLibModule}
You can use this GitHub repository for reference which demonstrates the setup and integration of the custom libraries using npm link
.您可以使用此GitHub 存储库作为参考,它演示了使用
npm link
设置和集成自定义库。
Angular CLI has a different mechanism for building libraries, based on ng-packagr
unlike for building applications, which uses @angular-devkit/build-angular
and is based on webpack
. Angular CLI 有一种不同的机制来构建库,它基于
ng-packagr
不同于构建应用程序,后者使用@angular-devkit/build-angular
并且基于webpack
。
The build system for libraries is only added to your dependencies when you add a library using ng generate library my-lib
.只有当您使用
ng generate library my-lib
添加库时,库的构建系统才会添加到您的依赖项中。 If you've been manually setting up the infrastructure, you might wanna make sure of this by verifying your config files.如果您一直在手动设置基础设施,您可能希望通过验证您的配置文件来确保这一点。
Since there is a difference in the build mechanism, the TypeScript source gets converted to a totally different JavaScript code in the built library than it would in a built application.由于构建机制存在差异,TypeScript 源代码在构建库中被转换为与构建应用程序中完全不同的 JavaScript 代码。
For this reason, an app that depends on a library should only use TypeScript path mappings that point to the built library.出于这个原因,依赖于库的应用程序应该只使用指向构建库的 TypeScript 路径映射。 TypeScript path mappings should not point to the library source .ts files.
TypeScript 路径映射不应指向库源 .ts 文件。
As stated in the official docs -正如官方文档中所述 -
.. When you build your own library, it has to find the mapping in your tsconfig paths.
.. 当您构建自己的库时,它必须在您的 tsconfig 路径中找到映射。
NOTE: Generating a library with the Angular CLI automatically adds its path to the tsconfig
file.注意:使用 Angular CLI 生成库会自动将其路径添加到
tsconfig
文件中。 The Angular CLI uses the tsconfig
paths to tell the build system where to find the library. Angular CLI 使用
tsconfig
路径告诉构建系统在哪里可以找到库。
Every time a file within the source code is changed, a partial build is performed that emits the new changes from the source code.每次更改源代码中的文件时,都会执行部分构建,以从源代码发出新更改。
If you believe your changes in the library code are not reflected in your app, your app is probably using an old build of the library.如果您认为库代码中的更改未反映在您的应用程序中,则您的应用程序可能使用的是旧版本的库。
You can rebuild your library whenever you make changes to it, but this extra step takes time.您可以在对库进行更改时重建库,但此额外步骤需要时间。 We can make use of Angular built-in Incremental build feature.
我们可以利用 Angular 内置的增量构建功能。 Incremental builds can be run as a background process in your dev environment.
增量构建可以作为开发环境中的后台进程运行。
Add the --watch
flag to the build command: $ ng build my-lib --watch
将
--watch
标志添加到构建命令: $ ng build my-lib --watch
since the op has been edited and is looking for creating a local link, the following explains only the second bit.由于操作已被编辑并正在寻找创建本地链接,因此以下仅解释第二位。 To publish your library on
npm-registry
, you can refer to the official angular docs on publishing libraries on npm registry .要在
npm-registry
上发布您的库,您可以参考关于在 npm registry 上发布库的官方角度文档。 If you're new to the npm registry, and this is the first time you're publishing a library, you might wanna check out the various grounds to cover when publishing your libraries on npm registry如果您是 npm 注册表的新手,并且这是您第一次发布库,您可能想查看在 npm 注册表上发布库时要涵盖的各种理由
While working with npm link
...在使用
npm link
...
Once you build your library, in order to verify if it's done correctly, go to the dist/ folder of your library workspace and verify the package.json
file.构建库后,为了验证它是否正确完成,请转到库工作区的 dist/ 文件夹并验证
package.json
文件。 The package.json
, should have an attribute main
. package.json
应该有一个属性main
。 Make sure it is linked with a file extension of .js and not a .ts确保它与.js而不是.ts的文件扩展名链接
Now you can use npm link
within your library workspace.现在您可以在库工作区中使用
npm link
。 It creates a reference pointer - a symlink - within the local node environment directing towards your library.它在指向您的库的本地节点环境中创建一个引用指针 -一个符号链接。
Once the local reference is created, go to your project workspace, and use npm link <library-name>
.创建本地引用后,转到您的项目工作区,并使用
npm link <library-name>
。 Make sure, it's the library name and not the library-workspace name .确保它是库名称而不是库工作区名称。
In the project workspace, within the node_modules
you should be able to see your library with an ' @ ' appended to it.在项目工作区中,在
node_modules
您应该能够看到附加了“ @ ”的库。 Now you can easily import relevant components and services from your library into your projects app.module.ts
.. or lazy-load them if you prefer that approach.现在,您可以轻松地将库中的相关组件和服务导入您的项目
app.module.ts
.. 或延迟加载它们(如果您喜欢这种方法)。
Likewise..同样地..
import { FooModule } from 'foo-library';
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.