angularng-modulesangular-standalone-components

What is the main benefit from NgModule in Angular 17


Now with Angular 17 I understand that you don't need to use the NgModule to run the different components in the application, because you can import them in the app.component.ts. With this approach; what is the main benefit of keep using the NgModule on your app?


Solution

  • In new Angular apps, you should only use standalone components. Everything which can be done with NgModules, can also be done with standalone components/pipes/directives, but not vice versa. The key benefits of standalone are:

    The only case I see where NgModules might be beneficial, is when you have a big feature consisting of several components/directives/pipes. Here a module can help to collect them, instead of needing to import all separately.

    For example, if you have a complex table feature and always need to import the following components:

    imports: [
      MyTableComponent,
      MyTableHeaderRowComponent,
      MyTableRowComponent,
      // ...
    ]
    

    Here it would be easier if there was a module:

    @NgModule({
      imports: [
        MyTableComponent,
        MyTableHeaderRowComponent,
        MyTableRowComponent,
        // ...
      ]
    })
    export class MyTableModule {}
    

    And when using the feature you could simply import MyTableModule. The individual components however should be standalone nevertheless, for the reasons mentioned above.