Performance: Migrating from NgModules
This affects the initial page load speed and runtime rendering performance by changing how Angular loads and compiles components.
Jump into concepts and practice - no test required
import { bootstrapApplication } from '@angular/platform-browser'; import { AppComponent } from './app.component'; bootstrapApplication(AppComponent);
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 {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| NgModules | No direct change | No direct change | Blocks initial paint until module compiles | [X] Bad |
| Standalone Components | No direct change | No direct change | Faster initial paint by skipping module compilation | [OK] Good |
standalone: true inside the @Component decorator.standalone: false, another uses invalid module property, and one misuses @NgModule.standalone: true inside @Component decorator [OK]import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
template: `{{ title }}
`
})
export class AppComponent {
title = 'Hello Angular Standalone';
}
bootstrapApplication(AppComponent);bootstrapApplication with a standalone component boots the app correctly.<h1>{{ title }}</h1> and title is set to 'Hello Angular Standalone'.import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
template: `Welcome
`
})
export class AppComponent {}
bootstrapApplication(AppComponent, {
providers: []
});imports array inside @Component.*ngIf, which requires CommonModule in imports.imports: [CommonModule] for directives like *ngIf [OK]1. UsebootstrapApplication()instead ofplatformBrowserDynamic().bootstrapModule()2. Addstandalone: trueto components 3. Useimportsarray inside@Componentfor dependencies 4. KeepNgModuledeclarations as before
bootstrapApplication() to start the app without NgModules.standalone: true to components to remove NgModule dependency.imports array inside @Component to include needed modules or components.