如何将角度项目设置为另一个角度项目的package.json中的依赖项 [英] How to setup angular project as a dependency in package.json of another angular project
问题描述
我有三个不同的Angular cli项目(X,Y,Z).我想将[X]作为父项目,而我想将Y和Z作为npm软件包依赖项添加到X.这意味着[X] package.json将包含[Y]和[Z]的依赖项,如下所示.
I have three different Angular cli projects (X, Y, Z). I want to make [X] as a parent project while I want to add Y and Z as npm package dependencies to X. That means [X] package.json will contain the dependencies of [Y] and [Z] as follows.
"dependencies": {
"@angular/common": "^4.0.0",
//.. other angular dependency
"y": "1.0.1",
"z": "1.0.3"
}
如何创建这些依赖项?
注意:现在,我在X中有一个Y和Z作为延迟加载的文件夹.我想将其作为独立的npm包解耦.
Note: Right now I have Y and Z as a lazy loaded folder inside X. Which I want to decouple as independent npm package.
推荐答案
基本上,您希望分3个步骤进行操作:
Basically you want to do this in 3 steps:
- 将项目"y"和"z"转换为库
- 将该库打包为npm-package
- 让"x"使用该库
简而言之,这是您的操作方式:
Here is how you do it in short:
-
我强烈建议使用 ng-packa 来创建库"y"和"z".
ng-packagr
允许您采用现有的Angular CLI
项目并将其转换为库.基本上,您必须做五件事:
I highly recommend using ng-packagr for creating the library out of "y" and "z".
ng-packagr
allows you to take an existingAngular CLI
project and turn it into a library. Basically you have to do five things:
- 安装程序包:
npm install ng-packagr --save-dev
-
添加具有以下内容的
ng-package.json
:
{
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
"lib": {
"entryFile": "public_api.ts"
}
}
添加public_api.ts
与模块的导出,即
add public_api.ts
with the exports of your modules, i.e.
export * from './src/app/modules/example/example.module'
将脚本添加到package.json
:
"packagr": "ng-packagr -p ng-package.json"
通过运行npm pack
创建npm软件包,这将生成一个y-1.0.0.tgz
文件,其中y
是您的项目名称,而1.0.0
是您在package.json
中设置的版本
Create the npm-package by running npm pack
which will generate a y-1.0.0.tgz
-file, where y
is your projects name and 1.0.0
the version you set in your package.json
现在,您可以通过运行npm install ./relative/path/to/y-1.0.0.tgz
将其作为依赖项安装到项目'x'中,并完成!
Now you can install this as dependency in your project 'x' by running npm install ./relative/path/to/y-1.0.0.tgz
and you're done!
此帖子基于此 查看全文