How to Fix Angular Component Not Found
In this tutorial, you'll learn about How to Fix Angular Component Not Found. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Angular throws Component 'AppComponent' is not included in a module or the component does not render when the custom element selector is used in a template, typically from a missing declaration or incorrect selector.
Quick Fix
Step 1: Declare the component in an NgModule
Components must be declared in exactly one module:
import { Component } from '@angular/core';
@Component({
selector: 'app-user',
template: '<p>User component</p>'
})
export class UserComponent { }
If UserComponent is not declared in any module, it cannot be used. Add it:
@NgModule({
declarations: [
UserComponent
]
})
export class AppModule { }
Step 2: Use the correct selector
The selector must match what is in the template:
@Component({
selector: 'user-profile',
template: '...'
})
Usage in template:
<user-profile></user-profile>
Not <app-user-profile> or <UserProfile>.
Step 3: Export the component for use in other modules
Components declared in a module are not visible outside it unless exported:
@NgModule({
declarations: [UserComponent],
exports: [UserComponent] // Make it available to importing modules
})
export class UserModule { }
Step 4: Import the module that declares the component
The using module must import the declaring module:
@NgModule({
imports: [UserModule], // Now UserComponent is usable
declarations: [AppComponent]
})
export class AppModule { }
Step 5: Check for standalone components (Angular 14+)
Standalone components do not need NgModule:
@Component({
selector: 'app-standalone',
template: '<p>Standalone works</p>',
standalone: true
})
export class StandaloneComponent { }
Use directly by importing:
@Component({
selector: 'app-root',
standalone: true,
imports: [StandaloneComponent],
template: '<app-standalone />'
})
export class AppComponent { }
Prevention
- Generate components with Angular CLI:
ng generate component user. - Keep one component per file for clarity.
- Use the
--moduleflag when generating to auto-declare. - Use standalone components to avoid NgModule boilerplate.
- Export reusable components from shared modules.
Common Mistakes with component not found
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world Angular code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro