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
  • 15,555
  • 19
  • 91
  • 184

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
  • 3,046
  • 2
  • 23
  • 34
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
  • 101,079
  • 60
  • 333
  • 488