简体   繁体   English

如何判断Angular2模块是否可以导入共享模块

[英]How to tell if an Angular2 module is ok to import into shared module

Is there a litmus test for determining if a module is a provider or exports other modules that are providers; 是否存在用于确定模块是提供者还是导出其他提供者模块的试金石; for determining if a module is a candidate for being listed in the "shared" module? 用于确定模块是否是在“共享”模块中列出的候选模块?

From angular.io: 来自angular.io:

"The SharedModule should not have providers for reasons explained previously. Nor should any of its imported or re-exported modules have providers. If you deviate from this guideline, know what you're doing and why." “由于之前解释的原因,SharedModule不应该有供应商。它的任何导入或重新导出的模块也不应该有提供者。如果你偏离了这个指导方针,就要知道你在做什么以及为什么。”

https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-module-recommendations https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-module-recommendations

However if I go to use a module, let's say the ReactiveFormsModule or the RoutingModule, how do I determine if that passes the test described above? 但是,如果我去使用模块,让我们说ReactiveFormsModule或RoutingModule,我如何确定它是否通过了上述测试? If I am only to use modules in the SharedModule that do not have providers or imported modules that re-export providers, then surely there must be some way to tell if a module meets this requirement. 如果我只使用SharedModule中没有提供者或导入模块重新导出提供者的模块,那么肯定必须有一些方法来判断模块是否满足此要求。 What is it? 它是什么?

Services are generally intended to be Singletons with application-wide scope. 服务通常是具有应用范围的单身人士。

When a module is imported, the Services provided by the imported module are added to the host module's injector. 导入模块时,导入模块提供的服务将添加到主机模块的注入器中。 By having services within a shared module, there is a real danger of multiple modules importing the shared module and creating multiple copies of the service, each with module scope. 通过在共享模块中提供服务,存在多个模块导入共享模块和创建服务的多个副本的真实危险,每个模块具有模块范围。 Make sure that if you add a service to a SharedModule, that this is what you intended. 如果您向SharedModule添加服务,请确保这是您的意图。

According to best practices, you should have a set of Core modules per application. 根据最佳实践,每个应用程序应该有一组Core模块。 These Core modules are not shared (it should only be imported by the AppModule) so it would be safe to add Services to the Core modules without breaking the Singleton intent. 这些Core模块不是共享的(它应该只能由AppModule导入),因此在不破坏Singleton意图的情况下将服务添加到Core模块是安全的。

The litmus test: 石蕊试验:

If you want Singleton Services that are shared application-wide, then don't put them in a SharedModule because SharedModules could be imported from multiple modules within the same application. 如果您需要在应用程序范围内共享的Singleton Services,则不要将它们放在SharedModule中,因为可以从同一应用程序中的多个模块导入SharedModule Instead put them in CoreModules , which should only be imported by the AppModule. 而是将它们放在CoreModules中 ,它只能由AppModule导入。

Remember the following conventions and guidelines: 请记住以下约定和准则:

  1. There is only one AppModule per Application 每个应用程序只有一个AppModule
  2. AppModules can import other modules AppModules可以导入其他模块
  3. Core modules should only be imported by the AppModule 核心模块只能由AppModule导入
  4. Shared modules can be imported by any module (not just the AppModule) 共享模块可以由任何模块导入(不仅仅是AppModule)

Common ways of doing things are as in the below image. 常见的做事方式如下图所示。

在此输入图像描述

Usually providers are always listed in the providers[] of the respective module if their scope is limited to that particular module, else move it to the Root module AppModule. 通常,如果providers[]的范围仅限于该特定模块,则它们始终列在相应模块的providers[]中,否则将其移至Root模块AppModule。

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

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