DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

7

Easy steps to migrate all Angular components to standalone

To make all components in your Angular project standalone, you can use the following steps:

  1. Update your angular.json file to set the standalone flag to true for all component schematics:
// angular.json
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "standalone": true
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Run the following command to migrate all existing components to standalone:

ng g @angular/core:standalone --all
Enter fullscreen mode Exit fullscreen mode

This will add the standalone: true flag to all component decorators and update the component imports as needed.

Verify that your application builds and runs correctly.
Once you have made all components standalone, you can remove any unnecessary NgModules from your application.

Here is an example of how to remove an NgModule:

  1. Open the NgModule file in a text editor.
  2. If the NgModule does not bootstrap a component, you can simply delete the file.
  3. If the NgModule does bootstrap a component, you need to move the component declaration to the main.ts file.

Once you have removed all unnecessary NgModules, you will have a fully standalone Angular application.

Please note that the ng g @angular/core:standalone --all command may not be able to migrate all components perfectly. You may need to make manual changes to some components.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay