简体   繁体   English

在Angular X的子模块中使用AppModule中的组件(X代表2+)

[英]Use component from AppModule in a child module in Angular X (X stands for 2+)

I have created a small component ( LoadingComponent ) in the root of my application and declared it (obviously) in my AppModule . 我已经在我的应用程序的根目录中创建了一个LoadingComponent件( LoadingComponent ),并在我的AppModule明确地声明了它。 This component is used when my application is loading and should show some fancy loading animations. 我的应用程序加载时会使用此组件,并应显示一些奇特的加载动画。

Now I want to use it in a child module when I save something. 现在我想在保存一些东西时在子模块中使用它。 But I am always getting errors when I want to use this component in another module. 但是当我想在另一个模块中使用这个组件时,我总是会遇到错误。

I exported the LoadingComponent in my AppModule: 我在AppModule中导出了LoadingComponent:

 import { ButtonsModule } from './buttons/buttons.module'; import { FormsModule } from '@angular/forms'; import { StatusLightsDisplayComponent } from './status-lights-display/status-lights-display.component'; import { StatusLightsContainerComponent } from './status-lights-container/status-lights-container.component'; import { HttpModule } from '@angular/http'; import { ReportsService } from './services/reports.service'; import { BrowserModule } from '@angular/platform-browser'; import { forwardRef, NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { GeneralInformationComponent } from './general-information/general-information.component'; import { TextfieldsComponent } from './textfields/textfields.component'; import { LoadingComponent } from './loading/loading.component'; @NgModule({ declarations: [ AppComponent, GeneralInformationComponent, TextfieldsComponent, StatusLightsContainerComponent, StatusLightsDisplayComponent, LoadingComponent ], imports: [ BrowserModule, HttpModule, FormsModule, ButtonsModule ], exports: [ LoadingComponent ], providers: [ ReportsService ], bootstrap: [AppComponent] }) export class AppModule { } 

The module I want to use the LoadingComponent is called ButtonsModule . 我想使用LoadingComponent的模块叫做ButtonsModule So I tried to import the AppModule in my ButtonsModule . 所以我尝试在我的AppModule中导入ButtonsModule But I am getting the error: Unexpected value 'undefined' imported by the module 'ButtonsModule' 但我收到错误: Unexpected value 'undefined' imported by the module 'ButtonsModule'

Here is my ButtonsModule: 这是我的ButtonsModule:

 import { AppModule } from '../app.module'; import { LoadingComponent } from '../loading/loading.component'; import { BottomComponent } from './bottom/bottom.component'; import { CalendarComponent } from './top/calendar/calendar.component'; import { CommonModule } from '@angular/common'; import { ExportService } from '../services/export.service'; import { forwardRef, NgModule } from '@angular/core'; import { FunctionsComponent } from './functions/functions.component'; import { NavArrowsComponent } from './shared/nav-arrows/nav-arrows.component'; import { SaveButtonComponent } from './shared/save-button/save-button.component'; import { TopComponent } from './top/top.component'; @NgModule({ imports: [ CommonModule, AppModule ], exports: [ TopComponent, FunctionsComponent, BottomComponent ], declarations: [ TopComponent, BottomComponent, FunctionsComponent, NavArrowsComponent, SaveButtonComponent, CalendarComponent ], providers: [ ExportService ] }) export class ButtonsModule { } 

I guess some of you already recognize some fail here :) But please read it to the end. 我想你们中的一些人已经认识到有些失败了:)但请将它读到最后。

I know the best practice here would be to create a shared module and then import this in my AppModule and the ButtonsModule , but this seems to be a little overkill, just for such a small component and it would also be my only shared module here. 我知道这里最好的做法是创建一个共享模块,然后在我的AppModuleButtonsModule导入它,但这似乎有点过分,只是对于这么小的组件,它也是我唯一的共享模块。 It would also create a lot of overhead. 它也会产生很多开销。

My questions: 我的问题:

  • Am I doing something wrong here? 我在这里做错了吗? If yes, what is it? 如果是,那是什么?
  • Would be the way, by creating a shared module, the right one? 通过创建共享模块,正确的方式是什么?
  • Why is my approach not working? 为什么我的方法不起作用? I mean, what is forbidding my approach under the hood of Angular and why? 我的意思是,什么是禁止我在Angular引擎盖下的方法,为什么?

NgModules help organize an application into cohesive blocks of functionality. NgModules帮助将应用程序组织成具有凝聚力的功能块。

Every Angular app has a root module class. 每个Angular应用程序都有一个根模块类。 By convention, the root module class is called AppModule and it exists in a file named app.module.ts. 按照惯例,根模块类称为AppModule,它存在于名为app.module.ts的文件中。

What if I import the same module twice? 如果我导入两次相同的模块怎么办?

That's not a problem. 那不是问题。 When three modules all import Module 'A', Angular evaluates Module 'A' once, the first time it encounters it, and doesn't do so again. 当三个模块全部导入模块'A'时,Angular会在第一次遇到模块'A'时评估模块'A',而不是再次进行模块'A'。

That's true at whatever level A appears in a hierarchy of imported modules. 无论A级出现在导入模块的层次结构中,都是如此。 When Module 'B' imports Module 'A', Module 'C' imports 'B', and Module 'D' imports [C, B, A], then 'D' triggers the evaluation of 'C', which triggers the evaluation of 'B', which evaluates 'A'. 当模块'B'导入模块'A',模块'C'导入'B',模块'D'导入[C,B,A],然后'D'触发'C'的评估,触发评估'B',评估'A'。 When Angular gets to the 'B' and 'A' in 'D', they're already cached and ready to go. 当Angular到达'B'中的'B'和'A'时,它们已经被缓存并准备好了。

Angular doesn't like modules with circular references, so don't let Module 'A' import Module 'B', which imports Module 'A'. Angular不喜欢带循环引用的模块,所以不要让模块'A'导入模块'B',它导入模块'A'。

Link 链接

Everything at the end compiles down to the Main App Module and importing it again in same module make the above condition true of circular dependency. 最后的所有内容都汇总到主应用程序模块并在同一模块中再次导入它使上述条件成为循环依赖的真实情况。 And being the main Module it should ideally not have any exports to be used by other Modules. 作为主要模块,理想情况下,其他模块不应使用任何导出。

Make a components module whose only job is to gather up the components, and export them, and import it in both places. 创建一个组件模块,其唯一的工作是收集组件,然后导出它们,并在两个位置导入它。 At first it seems painful but then you realize it helps to organize what is used where and will scale. 起初它似乎很痛苦,但后来你意识到它有助于组织使用的地方和规模。

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

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