Performance: Root module (AppModule) structure
This affects the initial page load speed and rendering performance by controlling how much code and how many components load at startup.
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', 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 |
|---|---|---|---|---|
| Import all feature modules in AppModule | Low (few nodes initially) | Low (initial load only) | High (delayed paint due to JS parsing) | [X] Bad |
| Lazy load feature modules via Router | Low (few nodes initially) | Low (initial load only) | Low (faster paint) | [OK] Good |
AppModule in an Angular application?bootstrap property in @NgModule for starting the app with AppComponent?bootstrap: [AppComponent] is the correct way to provide an array with the main component.AppModule snippet, what will happen when the app starts?import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule { }AppModule code:@NgModule({
declarations: AppComponent,
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule { }HeaderComponent to your Angular app. Which changes must you make in AppModule to include it properly?Pass the test and this pattern moves unknown