Performance: NgModule decorator and metadata
This affects the initial load time and rendering speed by controlling how Angular bundles and initializes modules and components.
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { RouterModule } from '@angular/router'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, RouterModule.forRoot([ { path: 'heavy-feature', loadChildren: () => import('./heavy-feature/heavy-feature.module').then(m => m.HeavyFeatureModule) } ])], bootstrap: [AppComponent] }) export class AppModule {}
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { HeavyFeatureModule } from './heavy-feature/heavy-feature.module'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, HeavyFeatureModule], bootstrap: [AppComponent] }) export class AppModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eagerly importing large modules | No direct DOM impact | No reflows triggered by NgModule itself | Blocks initial paint due to large bundle | [X] Bad |
| Lazy loading feature modules | No direct DOM impact | No reflows triggered by NgModule itself | Speeds up initial paint by reducing bundle size | [OK] Good |
NgModule decorator in Angular?NgModule decorator organizes related parts of an Angular app like components and services.Component define templates, but NgModule groups these parts together.NgModule decorator?declarations and imports, both expecting arrays.components or missing arrays.AppModule.bootstrap.length?
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule {}@NgModule({
declarations: MyComponent,
imports: [BrowserModule]
})
export class MyModule {}declarations property must be an array, but here it is a single component without brackets.imports is correctly an array. Bootstrap is optional, so no error there.NgModule metadata properties must you include to achieve this?imports.providers. To start the app with a component, use bootstrap.Pass the test and this pattern moves unknown