1

What is the use of the class in NgModule in Angular? In @Component, the class can contain variables of the component. What useful information could the class in NgModule contain?

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Manu Chadha
  • 13,750
  • 13
  • 62
  • 153

2 Answers2

0

Your class decorated with @NgModule will be empty, it is a class just to be exported to main.ts (probably), to bootstrap your application.

And others Modules are exported as classes so they can be imported into your root module.

Ramon Marques
  • 2,876
  • 2
  • 21
  • 33
0

I use module class constructor for some configuration initialization:

@NgModule({...})
export class RecordEditorViewComponentModule {
  constructor(private viewComponent: ViewComponent) {
    viewComponent.registerComponent({

You can also use module methods to register the same module with different set of providers so several instances of a module will be instantiated. For example, the way routing module does that. Router module provides two distinct methods to register module - forRoot and forChild. They both register the same module but with different set of providers. See this answer for more details.

Max Koretskyi
  • 93,980
  • 52
  • 302
  • 446