Par*_*osh 6 npm sinopia angular-cli angular
我有三个不同的Angular cli项目(X,Y,Z).我想将[X]作为父项目,而我想将Y和Z作为npm包依赖项添加到X.这意味着[X] package.json将包含[Y]和[Z]的依赖关系,如下所示.
"dependencies": {
"@angular/common": "^4.0.0",
//.. other angular dependency
"y": "1.0.1",
"z": "1.0.3"
}
Run Code Online (Sandbox Code Playgroud)
如何创建这些依赖项?
注意:现在我将Y和Z作为X中的延迟加载文件夹.我想将其解耦为独立的npm包.
基本上你想通过 3 个步骤来做到这一点:
简而言之,您可以这样做:
我强烈建议使用ng-packagr从“y”和“z”创建库。ng-packagr允许您获取现有Angular CLI项目并将其转换为库。基本上你必须做五件事:
npm install ng-packagr --save-dev添加ng-package.json以下内容:
Run Code Online (Sandbox Code Playgroud){ "$schema": "./node_modules/ng-packagr/ng-package.schema.json", "lib": { "entryFile": "public_api.ts" } }
添加public_api.ts模块的导出,即
Run Code Online (Sandbox Code Playgroud)export * from './src/app/modules/example/example.module'
将脚本添加到package.json:
"packagr": "ng-packagr -p ng-package.json"
npm run packagr通过运行npm pack来创建 npm-package ,它将生成一个y-1.0.0.tgz-file,其中y是您的项目名称和1.0.0您在您的项目中设置的版本package.json
现在,您可以通过运行将其安装为项目“x”中的依赖项,您npm install ./relative/path/to/y-1.0.0.tgz就大功告成了!
这篇文章是基于这篇文章。
小智 0
您需要打包 Y 和 Z 项目并将它们发布到 npm 存储库上,或者您可以在本地开发它并通过 npm 链接使用它们...这里的 Yeoman 生成器可以帮助您。
https://github.com/jvandemo/generator-angular2-library
| 归档时间: |
|
| 查看次数: |
4504 次 |
| 最近记录: |