简体   繁体   English

预先加载的模块-是否在核心模块中导入或导出?

[英]Eagerly loaded module - Imported or Exported in core module?

In order to make my AppModule cleaner, I imported an eagerly loaded feature module in the CoreModule, which is imported (once) in the AppModule. 为了使我的AppModule更加整洁,我在CoreModule中导入了一个渴望加载的功能模块,该模块(一次)导入了AppModule。

What i've found interesting, is that the app works by either exporting or importing the feature module in the CoreModule. 我发现有趣的是,该应用程序可以通过在CoreModule中导出或导入功能模块来工作。 Can anyone explain the difference? 谁能解释其中的区别? What's the correct way? 正确的方法是什么?

Well, if there's a feature(mostly a declarable like a Component, Directive or Pipe) that you have in that FeatureModule and you want to use it in your CoreModule (which you've imported in your AppModule ) or AppModule , you'll have to export it from your CoreModule as well. 好吧,如果该FeatureModule具有某个功能(通常是一个可声明的组件,如Component,Directive或Pipe),并且您想在CoreModule (已将其导入到AppModule )或AppModule使用它,您将拥有也可以从CoreModule导出它。

If you export something from a Module it would be available to use in a Module that you import that Module in. 如果您从某个模块导出某些内容,则可以在导入该模块的模块中使用它。

And if you export that module from the module that you've imported it in, you'll be able to use the exported module's features in the module that you're importing this module in. 而且,如果您从导入模块的模块中导出该模块,则可以在导入该模块的模块中使用导出模块的功能。

Let's take for eg, an example with a CoreModule , an AppModule and a FeatureModule . 让我们以带有CoreModuleAppModuleFeatureModule的示例为例。

The FeatureModule has a FeatureComponent that's declared in it. FeatureModule具有在其中声明的FeatureComponent If you want to use this FeatureComponent in the CoreModule , you'll have to export the FeatureComponent from the FeatureModule and then import the FeatureModule in your CoreModule . 如果要在CoreModule使用此FeatureComponent ,则必须从FeatureModule导出FeatureComponent ,然后在CoreModule导入FeatureModule

Now if you want to use the FeatureCompoent in your AppModule as well. 现在,如果您也想在AppModule使用FeatureCompoent you can simply export the FeatureModule from the CoreModule . 您只需从CoreModule export FeatureModule即可。 And since you've already imported the CoreModule in the AppModule you'll have access to all the exported members of the CoreModule and FeatureModule is one of them. 而且,由于您已经将CoreModule导入了AppModule中, AppModule您可以访问CoreModule所有导出成员, FeatureModule就是其中之一。

Here's what Angular's Docs say, to help you understand better: 这是Angular的文档所说的,以帮助您更好地理解:

The set of components, directives, and pipes declared in this NgModule that can be used in the template of any component that is part of an NgModule that imports this NgModule. 在此NgModule中声明的组件,指令和管道的集合,可以在作为导入此NgModule的NgModule的一部分的任何组件的模板中使用。 Exported declarations are the module's public API. 导出的声明是模块的公共API。

A declarable belongs to one and only one NgModule. 可声明的属于一个,并且仅属于一个NgModule。 A module can list another module among its exports, in which case all of that module's public declaration are exported. 一个模块可以在其导出中列出另一个模块,在这种情况下,该模块的所有公共声明都将导出。

Declarations are private by default. 声明默认为私有。 If this ModuleA does not export UserComponent, then only the components within this ModuleA can use UserComponent. 如果此ModuleA不导出UserComponent,则仅此ModuleA中的组件可以使用UserComponent。

ModuleA can import ModuleB and also export it, making exports from ModuleB available to an NgModule that imports ModuleA. ModuleA可以导入ModuleB,也可以将其导出,从而使ModuleB的导出可用于导入ModuleA的NgModule。

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

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