Feature modules help keep your Angular app neat and tidy by grouping related parts together. This makes your app easier to understand and work on.
Feature modules for organization in Angular
import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-feature', template: `<p>Feature works!</p>`, }) export class FeatureComponent {} import { NgModule } from '@angular/core'; import { FeatureComponent } from './feature.component'; @NgModule({ declarations: [FeatureComponent], imports: [CommonModule], exports: [FeatureComponent] }) export class FeatureModule {}
A feature module is an Angular module that groups related components, directives, and pipes.
Use NgModule to define a feature module and list its parts inside.
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { UserProfileComponent } from './user-profile.component'; @NgModule({ declarations: [UserProfileComponent], imports: [CommonModule], exports: [UserProfileComponent] }) export class UserProfileModule {}
import { NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { AdminDashboardComponent } from './admin-dashboard.component'; @NgModule({ declarations: [AdminDashboardComponent], imports: [RouterModule], exports: [AdminDashboardComponent] }) export class AdminModule {}
This example shows a feature module called FeatureModule that contains a simple component. The main app module imports this feature module and uses its component in the app.
import { Component } from '@angular/core'; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-feature', template: `<p>Feature module loaded!</p>` }) export class FeatureComponent {} @NgModule({ declarations: [FeatureComponent], imports: [CommonModule], exports: [FeatureComponent] }) export class FeatureModule {} @Component({ selector: 'app-root', template: `<h1>Welcome</h1><app-feature></app-feature>` }) export class AppComponent {} @NgModule({ declarations: [AppComponent], imports: [BrowserModule, FeatureModule], bootstrap: [AppComponent] }) export class AppModule {}
Feature modules help keep code organized and easier to maintain.
You can lazy load feature modules to improve app speed.
Always import CommonModule in feature modules to use common Angular directives like *ngIf and *ngFor.
Feature modules group related parts of an Angular app.
They make apps easier to manage and understand.
Import feature modules into the main app module to use their components.
Practice
Solution
Step 1: Understand feature module role
Feature modules group related parts like components and services to keep code organized.Step 2: Compare other options
Feature modules do not replace AppModule, are not only for routing, and do not store styles.Final Answer:
To group related components, directives, and services for better organization -> Option CQuick Check:
Feature modules organize related code = D [OK]
- Thinking feature modules replace AppModule
- Believing feature modules only handle routing
- Confusing feature modules with style storage
ProductsModule into the root module?Solution
Step 1: Recall Angular module metadata
Feature modules are imported using theimportsarray in @NgModule.Step 2: Understand other arrays
declarationsis for components/directives,providersfor services,bootstrapfor root component.Final Answer:
imports: [ProductsModule] -> Option BQuick Check:
Feature modules go in imports = B [OK]
- Putting modules in declarations array
- Adding modules to providers array
- Using bootstrap array for modules
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?Solution
Step 1: Understand module imports and declarations
SinceProductsModuleis imported inAppModule, its declared components are available inAppModule's templates.Step 2: Check component usage in template
Using<app-product-list>inAppComponentworks becauseProductListComponentis exported byProductsModule.Final Answer:
The ProductListComponent will render correctly inside AppComponent -> Option AQuick Check:
Imported module components usable in templates = A [OK]
- Thinking components must be declared in AppModule to use
- Assuming runtime crash instead of compile error
- Believing imported components won't render
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?Solution
Step 1: Check module import
IfOrdersModuleis not imported inAppModule, its components are unknown in root templates.Step 2: Review other options
While declaration or selector errors cause issues, the common cause for 'not a known element' is missing module import.Final Answer:
OrdersModule was not imported into AppModule -> Option AQuick Check:
Missing module import causes unknown element error = C [OK]
- Forgetting to import the feature module
- Adding module to declarations array
- Ignoring selector typos
UsersModule and AdminModule. AdminModule needs to use components from UsersModule. How should you set up the modules to allow this?Solution
Step 1: Understand module dependency direction
IfAdminModuleneedsUsersModulecomponents,UsersModulemust be imported intoAdminModule.Step 2: Export components from UsersModule
Only exported components fromUsersModuleare visible toAdminModule.Final Answer:
Import UsersModule into AdminModule and export needed components from UsersModule -> Option DQuick Check:
Import dependency module and export components = A [OK]
- Importing modules in wrong direction
- Declaring components in multiple modules
- Using providers to share components
Ready to prove it?
Pass the test and this pattern moves unknown
