Angular 15+ Architecture
Angular applications traditionally use NgModules to organize components, directives, pipes, and services. From Angular 15+, you can build apps using standalone components, which remove the need for NgModules, simplify project structure, and reduce boilerplate while still allowing gradual migration from existing module-based apps.
imports to bring in dependencies.In a traditional NgModule-based app, the root module (often AppModule) declares all components and bootstraps the root component. In the standalone approach, components set standalone: true and can be directly imported into other standalone components or bootstrapped with bootstrapApplication().
Standalone components still rely on Angular feature modules (like BrowserModule or CommonModule), but these are imported directly into the component rather than via an NgModule's imports.
This is the classic way of bootstrapping an Angular application using a root AppModule.
// app.module.ts - classic Angular root module
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
@NgModule({
declarations: [AppComponent, HomeComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Here, the HomeComponent is marked as standalone and is bootstrapped via bootstrapApplication() instead of being declared in an NgModule.
// home.component.ts - standalone component
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-home',
standalone: true,
imports: [CommonModule],
template: '<h2>Welcome Home</h2>'
})
export class HomeComponent { }
// main.ts - bootstrap using bootstrapApplication
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { HomeComponent } from './app/home/home.component';
bootstrapApplication(AppComponent, {
imports: [HomeComponent]
});
AppModule, looks at the declarations, and bootstraps AppComponent.HomeComponent is self-contained and imported directly in the bootstrapApplication() call.AppComponent, which can use <app-home> and display: <h2>Welcome Home</h2>.declarations array.CommonModule) in the imports array of the standalone component.AppModule and HomeComponent, then rewrite it using a standalone HomeComponent.HomeComponent and a module-based feature component.