简体   繁体   English

Angular 库与 Angular 模块在 monorepo 中的优势? NX架构

[英]Benefits of Angular libraries vs Angular modules in a monorepo? NX architecture

I'm wondering what are the benefits of working with libraries rather than modules in Angular, as nx.dev recommends for an monorepo architecture.我想知道在 Angular 中使用库而不是模块有什么好处,因为 nx.dev 建议使用 monorepo 架构。

I understand the benefits for a npm publishable feature like interfaces that another repo will consume, but why would I want to make a library out of a business related feature, like a homepage, for example :我理解 npm 可发布功能的好处,比如另一个 repo 将使用的接口,但是为什么我要从业务相关的功能中创建一个库,比如主页,例如:

myorg/
├── apps/
│   ├── todos/
│   └── todos-e2e/
├── libs/
      ├── todos/
        ├── home/   <-- why nx recommends making a library here?
          ├── src/
            ├── lib/
              ├── home.component.html/ts/scss
              ├── home.module.ts
├── tools/
├── README.md
├── workspace.json
├── nx.json
├── package.json

rather than而不是

myorg/
├── apps/
│   ├── todos/
         [...]
         ├── home/   <-- just a simple lazy loaded module here
           ├── home.component.html/ts/scss
           ├── home.module.ts
│   └── todos-e2e/
├── libs/
├── tools/
├── README.md
├── workspace.json
├── nx.json
├── package.json
└── tsconfig.json

NX recommends that you put more than 90% of your code in libs and the the purpose of those libs is not only sharing code between the different applications we can create an lib even if the code is not shared and use app folders only for configurations like routings and environments. NX 建议您将90%以上的代码放在库中,这些库的目的不仅是在不同的应用程序之间共享代码,我们还可以创建库,即使代码未共享,并且仅将应用程序文件夹用于配置,例如路线和环境。

we have to mention the benefits of separating your code into small units (libs) that you can test and build them individually .我们不得不提到将您的代码分成小单元(库)的好处,您可以单独测试和构建它们

so in your case it can be a lib that can be lazy loaded into your app it can manage it's own routes, here is the list of libs types taken from their book因此,在您的情况下,它可以是一个可以延迟加载到您的应用程序中的库,它可以管理自己的路由,这是从他们的书中获取的库类型列表

  • Feature libraries: Developers should consider feature libraries as libraries that implement smart UI (with injected services) for specific business use cases or pages in an application.功能库:开发人员应将功能库视为为应用程序中的特定业务用例或页面实现智能 UI(具有注入服务)的库。

  • UI libraries: A UI library contains only presentational components. UI 库:UI 库仅包含展示组件。

  • Data-access libraries: A data-access library contains services and utilities for interacting with a back-end system.数据访问库:数据访问库包含用于与后端系统交互的服务和实用程序。 It also includes all the code related to State management.它还包括所有与状态管理相关的代码。

  • Utility libraries: A utility library contains common utilities and services used by many libraries and applications.实用程序库:实用程序库包含许多库和应用程序使用的通用实用程序和服务。

when you see all those types of libs you understand that the idea is to have the max of code in libs insteand of apps.当你看到所有这些类型的库时,你就会明白这个想法是在库中拥有最多的代码而不是应用程序。

i invite you to check this when Victor Savkin talked about libsVictor Savkin谈到 libs 时,我邀请你检查这个

https://youtu.be/qYNiOKDno_I?t=6m35s https://youtu.be/qYNiOKDno_I?t=6m35s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM