如何在另一个 angular 项目的 package.json 中将 angular 项目设置为依赖项 [英] How to setup angular project as a dependency in package.json of another angular project

查看:26
本文介绍了如何在另一个 angular 项目的 package.json 中将 angular 项目设置为依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个不同的 Angular cli 项目(X、Y、Z).我想将 [X] 作为父项目,同时将 Y 和 Z 作为 npm 包依赖项添加到 X.这意味着 [X] package.json 将包含 [Y] 和 [Z] 的依赖项,如下所示.

依赖项":{"@angular/common": "^4.0.0",//.. 其他角度依赖"y": "1.0.1",z":1.0.3"}

如何创建这些依赖项?

注意:现在我将 Y 和 Z 作为 X 中的延迟加载文件夹.我想将其解耦为独立的 npm 包.

解决方案

基本上你想通过 3 个步骤来做到这一点:

  1. 将您的项目y"和z"变成一个库
  2. 将该库打包到 npm 包中
  3. 让x"使用那个库

简而言之,您可以这样做:

  1. 我强烈建议使用 ng-packagr 来创建库y"和z".ng-packagr 允许您将现有的 Angular CLI 项目转换为库.基本上你必须做五件事:

    • 安装包:npm install ng-packagr --save-dev
    • 添加ng-package.json,内容如下:

      <块引用>

      <代码>{"$schema": "./node_modules/ng-packagr/ng-package.schema.json",库":{"entryFile": "public_api.ts"}}

    • public_api.ts 添加到模块的导出中,即

      <块引用>

      export * from './src/app/modules/example/example.module'

    • package.json 添加脚本:"packagr": "ng-packagr -p ng-package.json"

    • 运行脚本:npm run packagr
  2. 通过运行 npm pack 创建 npm 包,这将生成一个 y-1.0.0.tgz 文件,其中 y 是您的项目名称,1.0.0 是您在 package.json

  3. 中设置的版本
  4. 现在你可以通过运行 npm install ./relative/path/to/y-1.0.0.tgz 把它作为依赖安装到你的项目x"中,你就完成了!

这篇文章基于这个 文章.

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"
}

How to create those dependencies?

Note: Right now I have Y and Z as a lazy loaded folder inside X. Which I want to decouple as independent npm package.

解决方案

Basically you want to do this in 3 steps:

  1. Turn your projects "y" and "z" into a library
  2. Pack that library into an npm-package
  3. Let "x" consume that library

Here is how you do it in short:

  1. I highly recommend using ng-packagr for creating the library out of "y" and "z". ng-packagr allows you to take an existing Angular CLI project and turn it into a library. Basically you have to do five things:

    • install package with: npm install ng-packagr --save-dev
    • add ng-package.json with the following content:

      {
         "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
         "lib": {
           "entryFile": "public_api.ts"
          }
      }
      

    • add public_api.ts with the exports of your modules, i.e.

      export * from './src/app/modules/example/example.module'
      

    • add script to package.json: "packagr": "ng-packagr -p ng-package.json"

    • run the script: npm run packagr
  2. 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

  3. 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!

This post is based on this article.

这篇关于如何在另一个 angular 项目的 package.json 中将 angular 项目设置为依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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