0
0
Angularframework~10 mins

Migrating from NgModules in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Migrating from NgModules
Start with NgModule-based app
Identify NgModules to migrate
Convert NgModules to standalone components
Replace NgModule imports with standalone imports
Update bootstrap to standalone component
Test app functionality
Done
This flow shows the steps to change an Angular app from using NgModules to using standalone components and imports.
Execution Sample
Angular
import { Component } from '@angular/core';

@Component({
  standalone: true,
  selector: 'app-root',
  template: `<h1>Hello Standalone!</h1>`
})
export class AppComponent {}
Defines a standalone Angular component replacing the need for an NgModule.
Execution Table
StepActionBeforeAfterEffect
1Identify NgModule AppModuleApp uses @NgModuleAppModule existsReady to migrate
2Convert AppComponent to standaloneAppComponent without standaloneAppComponent with standalone: trueNo longer needs NgModule declaration
3Remove AppModule importsAppModule imports other modulesNo AppModule, imports moved to componentsSimplifies imports
4Bootstrap standalone AppComponentBootstrap via AppModuleBootstrap via standalone AppComponentApp starts without NgModule
5Run appApp uses NgModule systemApp runs with standalone componentsMigration complete
💡 All NgModules replaced by standalone components and imports; app bootstrapped without NgModule.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
AppComponentNgModule componentStandalone componentStandalone componentStandalone componentStandalone component
AppModuleDefined and usedDefined and usedRemovedRemovedRemoved
Bootstrap methodNgModule bootstrapNgModule bootstrapNgModule bootstrapStandalone bootstrapStandalone bootstrap
Key Moments - 3 Insights
Why do we add 'standalone: true' to the component?
Adding 'standalone: true' tells Angular this component works without needing an NgModule, as shown in execution_table step 2.
How do we bootstrap the app without an NgModule?
Instead of bootstrapping the NgModule, we bootstrap the standalone component directly, as shown in execution_table step 4.
What happens to imports from NgModules after migration?
Imports move from NgModules to standalone components or are imported directly where needed, removing the NgModule import layer (execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the AppComponent converted to standalone?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check the 'Action' column for 'Convert AppComponent to standalone' in execution_table.
According to variable_tracker, what is the state of AppModule after step 3?
ARemoved
BDefined and used
CPartially removed
DStill bootstrapping
💡 Hint
Look at the 'AppModule' row under 'After Step 3' in variable_tracker.
If we forget to add 'standalone: true' to a component, what happens during migration?
AComponent still works standalone
BAngular throws an error or requires NgModule
CApp bootstraps normally
DImports are ignored
💡 Hint
Refer to key_moments about the importance of 'standalone: true' and execution_table step 2.
Concept Snapshot
Migrating from NgModules:
- Add 'standalone: true' to components
- Remove NgModule declarations and imports
- Bootstrap standalone components directly
- Move imports to standalone components
- Test app to confirm migration
Full Transcript
Migrating from NgModules in Angular means changing your app to use standalone components instead of NgModules. First, identify your NgModules. Then, add 'standalone: true' to your components to make them standalone. Remove NgModule imports and declarations. Change your bootstrap process to start with the standalone component instead of the NgModule. Finally, test your app to ensure it works without NgModules. This simplifies your app structure and uses Angular's modern features.