Angular CLI-使用"ng new library"创建的自定义库时,"Cannot find module" [英] Angular CLI - 'Cannot find module' when using custom library created with 'ng new library'

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

问题描述

我正在尝试使用 ng新库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构建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"创建的自定义库时,"Cannot find module"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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