Angular Components Types Structure and CLI
What is a Component?
- A component is a part of something.
- Example: Car
- Tyres
- Engine
- AC vents
- Dashboard
- Doors
- 👉 All these are components of a car.
- A component can also have sub-components.
- Example: Engine itself has cylinders, pipes, belts, nozzles.
Conclusion:
A big thing is made of components, and a component can also have smaller components inside it.
Components in Angular
- In Angular:
- Header → Component
- Footer → Component
- Login page → Component
- Signup page → Component
- Product listing → Component
- Profile page → Component
Why Do We Need Components? (Interview Question)
Question:
Why not write normal pages? Why components?
Answer:
- Components allow reusability.
- Just like functions in programming.
- Example:
- One seat design used multiple times in a car.
- One door design used on all sides.
- Same in coding:
- One Login component can be used:
- On a page
- Inside a dialog box
- Multiple places
LinkedIn Example
- Profile image appears:
- Header
- Sidebar
- Comments
- Same round image component reused everywhere.
Types of Components in Angular
Root Component
- Only one root component in an Angular app.
- Default root component:
- 👉
AppComponent - All other components exist inside it.
Feature Components
- Created for specific features:
- Login
- Signup
- Listing
- Profile
- Settings
Shared Components
- Components used in multiple places.
- Example:
- Profile image
- Buttons
- Search box
Standalone Components
- Before Angular 14:
- Components had to be registered in NgModule.
- Now (Angular 21):
- All components are standalone by default.
- No need for NgModule registration.
Dumb Components
- Only UI
- No logic
- Example:
- Animations
- Shapes
- Static display components
Smart Components
- Components with logic
- Example:
- Search with auto-suggestion
- Data handling
- API logic
- Can also be shared components.
Creating a Component
Command to Create Component
ng generate component profile
OR short form:
ng g c profile
- Angular CLI commands always start with
ng. - This creates a profile folder.
Component Structure (4 Important Files)
When a component is created, it contains:
HTML file – UI
- Buttons
- Input fields
- Headings
- Tags
TypeScript (.ts) – Logic
- Properties
- Functions
- Service calls
CSS file – Styling
- Colors
- Layout
- UI design
Spec file (.spec.ts) – Testing
- Unit test cases
- Used by developers
- Keyword spec means testing
Note:
- Not all projects use testing.
- Some projects skip unit testing.
Using a Component
Step 1: Use Selector in HTML
- Each component has a selector, for example:
<app-profile></app-profile>
- Use it inside another component’s HTML file.
Step 2: Import Component in TS File
- Import the component inside the parent component’s
.tsfile.
Example:
import { ProfileComponent } from './profile/profile.component';
Example Code Used in Video
profile.component.html
<h1>Profile Component</h1>
profile.component.css
h1 {
color: orange;
}
Output:
- Text appears
- Styled with orange color
Reusing the Component Multiple Times
<app-profile></app-profile>
<app-profile></app-profile>
<app-profile></app-profile>
<app-profile></app-profile>
➡️ Component renders multiple times.