[英]Angular CLI - 'Cannot find module' when using custom library created with 'ng new library'
I'm trying to consume my own library after creating it with the Angular CLI using ng new library lib-name
.我正在尝试使用 Angular CLI 使用
ng new library lib-name
创建我自己的库。
I'm trying to import the library as documented, this way:我正在尝试按记录导入库,这样:
import {MyLibModule} from 'ngx-mylib';
But I get the following error:但我收到以下错误:
error TS2307: Cannot find module 'ngx-mylib'.
I did ng build ngx-mylib
and also verified my tsconfig.json has the following paths (which are added automatically using the CLI):我做了
ng build ngx-mylib
并且还验证了我的 tsconfig.json 具有以下路径(使用 CLI 自动添加):
"paths": {
"ngx-mylib": [
"dist/ngx-chartjs"
],
"ngx-mylib/*": [
"dist/ngx-chartjs/*"
]
}
public-api.ts:公共api.ts:
export * from './lib/charts.service';
export * from './lib/chart.component';
export * from './lib/charts.module';
What I'm missing?我错过了什么?
After build your library you have to pack it.构建库后,您必须打包它。
Go to your dist folder and run Go 到您的 dist 文件夹并运行
npm pack
this will create a your-lib-name.tgz file and then you can use it on your projects.这将创建一个 your-lib-name.tgz 文件,然后您可以在您的项目中使用它。
Use it like this:像这样使用它:
npm install path-to-your-tgz-file/your-lib.tgz
You can also publish it to npm or to a private repository.您还可以将其发布到 npm 或私有存储库。
try to build it again and then run your project sometimes angular can't build the library properly尝试再次构建它然后运行您的项目有时 angular 无法正确构建库
Try adding MyLibModule to your app.module imports and exports.尝试将 MyLibModule 添加到您的 app.module 导入和导出中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.