Angular CLI - 使用通过“ng new library"创建的自定义库时“找不到模块" [英] Angular CLI - 'Cannot find module' when using custom library created with 'ng new library'

查看:23
本文介绍了Angular CLI - 使用通过“ng new library"创建的自定义库时“找不到模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 ng new library lib-name 使用 Angular CLI 创建后,我尝试使用我自己的库.

I'm trying to consume my own library after creating it with the Angular CLI using ng new library lib-name.

我正在尝试按照文档中的方式导入库,如下所示:

I'm trying to import the library as documented, this way:

import {MyLibModule} from 'ngx-mylib';

但我收到以下错误:

 error TS2307: Cannot find module 'ngx-mylib'.

我做了 ng build ngx-mylib 并验证了我的 tsconfig.json 具有以下路径(使用 CLI 自动添加):

I did ng build ngx-mylib and also verified my tsconfig.json has the following paths (which are added automatically using the CLI):

"paths": {
      "ngx-mylib": [
        "dist/ngx-chartjs"
      ],
      "ngx-mylib/*": [
        "dist/ngx-chartjs/*"
      ]
    }

public-api.ts:

public-api.ts:

export * from './lib/charts.service';
export * from './lib/chart.component';
export * from './lib/charts.module';

我缺少什么?

推荐答案

构建库后,您必须对其进行打包.

After build your library you have to pack it.

转到您的 dist 文件夹并运行

Go to your dist folder and run

npm pack

这将创建一个 your-lib-name.tgz 文件,然后您可以在您的项目中使用它.

this will create a your-lib-name.tgz file and then you can use it on your projects.

像这样使用它:

npm install path-to-your-tgz-file/your-lib.tgz

您也可以将其发布到 npm 或私有存储库.

You can also publish it to npm or to a private repository.

这篇关于Angular CLI - 使用通过“ng new library"创建的自定义库时“找不到模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆