Performance: Feature modules for organization
This affects initial page load speed and bundle size by splitting code into smaller chunks loaded on demand.
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { RouterModule } from '@angular/router'; @NgModule({ declarations: [AppComponent], imports: [ BrowserModule, RouterModule.forRoot([ { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) } ]) ], bootstrap: [AppComponent] }) export class AppModule {}
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { FeatureComponent } from './feature/feature.component'; @NgModule({ declarations: [AppComponent, FeatureComponent], imports: [BrowserModule], bootstrap: [AppComponent] }) export class AppModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single large module | Many nodes loaded upfront | Multiple reflows during initial render | High paint cost due to large DOM | [X] Bad |
| Feature modules with lazy loading | Fewer nodes initially | Single reflow on initial render | Lower paint cost initially | [OK] Good |
ProductsModule into the root module?imports array in @NgModule.declarations is for components/directives, providers for services, bootstrap for root component.app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ProductsModule } from './products/products.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, ProductsModule],
bootstrap: [AppComponent]
})
export class AppModule { }
What will happen if ProductsModule declares and exports a component ProductListComponent and you use <app-product-list> in AppComponent template?ProductsModule is imported in AppModule, its declared components are available in AppModule's templates.<app-product-list> in AppComponent works because ProductListComponent is exported by ProductsModule.OrdersModule but when you try to use its component OrderListComponent in AppComponent, Angular shows an error: 'app-order-list' is not a known element. What is the most likely cause?OrdersModule is not imported in AppModule, its components are unknown in root templates.UsersModule and AdminModule. AdminModule needs to use components from UsersModule. How should you set up the modules to allow this?AdminModule needs UsersModule components, UsersModule must be imported into AdminModule.UsersModule are visible to AdminModule.Pass the test and this pattern moves unknown