Performance: Declarations, imports, and exports
This concept affects the initial bundle size and module loading time, impacting how fast the Angular app starts and renders.
import { CommonModule } from '@angular/common'; @NgModule({ declarations: [ComponentA], imports: [CommonModule], exports: [ComponentA] }) export class ComponentAModule {}
import { CommonModule } from '@angular/common'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; import { RouterModule } from '@angular/router'; @NgModule({ declarations: [ComponentA, ComponentB, ComponentC], imports: [CommonModule, FormsModule, ReactiveFormsModule, HttpClientModule, RouterModule], exports: [ComponentA, ComponentB, ComponentC] }) export class SharedModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large shared module with many imports/exports | N/A | N/A | High due to delayed script execution | [X] Bad |
| Small focused module with minimal imports/exports | N/A | N/A | Low, scripts load and execute faster | [OK] Good |
declarations array inside an NgModule?declarations array is used to tell Angular which components, directives, and pipes belong to this module.CommonModule into an Angular module?CommonModule are added inside the imports array to bring their features in.imports: [CommonModule] is the correct syntax to import a module.import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './my.component';
@NgModule({
declarations: [MyComponent],
imports: [CommonModule],
exports: []
})
export class MyModule {}MyComponent?MyComponent is declared but not exported in MyModule.MyComponent is not exported, other modules cannot use it.import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyComponent } from './my.component';
@NgModule({
declarations: [],
imports: [BrowserModule, MyComponent],
exports: [MyComponent]
})
export class AppModule {}MyComponent must be listed in declarations, not imports.imports, components go in declarations. Exporting components is allowed.MyComponent should be in declarations, not imports -> Option DSharedModule declares and exports a component ButtonComponent. You want to use ButtonComponent inside FeatureModule. Which is the correct way to set this up?SharedModule exports ButtonComponent, so importing SharedModule in FeatureModule makes ButtonComponent available.ButtonComponent in FeatureModule; just import SharedModule.Pass the test and this pattern moves unknown