简体   繁体   English

共享模块中的角度导入/导出模块或多个模块中的导入

[英]Angular Import/Export modules in shared Module or Import in Multiple Modules

I am restructuring Angular application and I need to clarify some stuff. 我正在重组Angular应用程序,需要澄清一些东西。

I am using modular structure (Core, Shared, etc). 我正在使用模块化结构(Core,Shared等)。 There are some common modules which used almost everywhere for ex CommonModule, FormsModule and many others. 有一些通用模块几乎在所有地方都用于ex CommonModule,FormsModule和许多其他模块。 Practically I need to understand which approach would be more... native options are: 实际上,我需要了解哪种方法更好...本机选项是:

  1. To include these modules into the Shared module import, exports arrays (thus they can be used everywhere where I import shared module. 要将这些模块包括在共享模块导入中,请导出数组(因此可以在导入共享模块的任何地方使用它们。

  2. To import common modules in all submodules. 在所有子模块中导入通用模块。

Which approach to use? 使用哪种方法?

Use the first approach since it allows for better organization/streamlining of code as a project gets larger. 使用第一种方法,因为随着项目的扩大,它可以更好地组织/简化代码。 The Shared Module pattern is recommended by the Angular team . Angular团队推荐使用共享模块模式。

That said, there is nothing wrong with importing each common module into submodules, but it becomes cumbersome to do so as your project grows in size. 话虽如此,将每个通用模块导入子模块并没有错,但是随着项目规模的扩大,这样做很麻烦。 For a project with several modules, it will be extremely redundant to manually import commonly used modules into each newly created module, as opposed to making a single import. 对于具有多个模块的项目,将手工导入的常用模块导入每个新创建的模块将是非常多余的,而不是进行单个导入。

According to Angular Style Guide. 根据Angular样式指南。 It's better to choose Option1. 最好选择Option1。

You can refer to this document: Angular Stype Guide(Shared module) 您可以参考此文档: Angular Stype指南(共享模块)

Thanks a lot, it seems that importing child modules solved my problem,but than another issue arose. 非常感谢,导入子模块似乎解决了我的问题,但随后又出现了另一个问题。

I have also to import child route modules 我还必须导入子路线模块

In my case, it was because I had created a route file in a child module "A", but didn't import that route file in the root module, even though child module "A" was imported into the root module. 就我而言,这是因为我在子模块“ A”中创建了一个路由文件,但是没有将路由文件导入到根模块中,即使子模块“ A”已导入到根模块中。

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

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