Reusable Components
Reusable Components are Angular components designed to be used multiple times in different parts of an application. They help reduce duplicate code and make applications easier to maintain.
What is a Reusable Component?
A reusable component is a component created for a common UI or functionality that can be used in multiple places.
Instead of writing the same HTML and logic repeatedly, we create one component and reuse it whenever required.
Simple Example
A Student Card can be created once and reused for Sam, Pradnya and other students by passing different data.
Reusable Component Flow
Why Use Reusable Components?
Reuse
The same component can be used multiple times.
Less Code
Avoid writing the same UI code repeatedly.
Easy Maintenance
Changes can be made in one component.
Consistency
Reused components provide consistent UI.
Creating a Reusable Student Card
Suppose we want to display student information in the same card design throughout our Angular application.
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-student-card',
standalone: true,
template: `
<div class="student-card">
<h3>{{ name }}</h3>
<p>Course: {{ course }}</p>
</div>
`
})
export class StudentCardComponent {
@Input() name = '';
@Input() course = '';
}
Using the Reusable Component
The same component can now be used multiple times with different input values.
<app-student-card
name="Sam"
course="Angular Full Stack">
</app-student-card>
<app-student-card
name="Pradnya"
course="Java Full Stack">
</app-student-card>
Sam
Course: Angular Full Stack
Pradnya
Course: Java Full Stack
Reusable Component with Property Binding
Input values can also come from properties of the parent component.
export class AppComponent {
studentName = 'Sam';
studentCourse = 'Angular Full Stack';
}
<app-student-card
[name]="studentName"
[course]="studentCourse">
</app-student-card>
Sam
Course: Angular Full Stack
Using One Component Multiple Times
A reusable component can appear many times on the same page.
<app-student-card
name="Sam"
course="Angular">
</app-student-card>
<app-student-card
name="Pradnya"
course="React">
</app-student-card>
<app-student-card
name="Samiksha"
course=".NET Full Stack">
</app-student-card>
Sam
Course: Angular
Pradnya
Course: React
Samiksha
Course: .NET Full Stack
Example: Reusable Button Component
Reusability is not limited to student cards. Buttons, navigation items, alerts and other UI elements can also be created as reusable components.
import { Component, Input, Output, EventEmitter }
from '@angular/core';
@Component({
selector: 'app-button',
standalone: true,
template: `
<button (click)="clicked.emit()">
{{ label }}
</button>
`
})
export class ButtonComponent {
@Input() label = 'Click';
@Output() clicked = new EventEmitter<void>();
}
<app-button
label="Save Student"
(clicked)="saveStudent()">
</app-button>
Reusable Component Architecture
Common Reusable Components
Student Card
Displays student information.
Form Control
Reusable input or form elements.
Notification
Displays application messages.
Button
Reusable action button.
Reusable Component Design
Advantages of Reusable Components
- Code Reusability: Write common functionality once and reuse it.
- Consistency: The same component provides a consistent design.
- Maintainability: Changes can be made in a single component.
- Scalability: Large applications can be built using smaller components.
- Testing: Small components are easier to test independently.
Best Practices
- Keep reusable components focused on one responsibility.
-
Use
@Inputfor dynamic data instead of hard-coding values. -
Use
@Outputwhen the reusable component needs to notify its parent. - Give reusable components clear and meaningful names.
- Avoid putting unrelated business logic inside UI components.
- Keep reusable components independent whenever practical.
Summary
Reusable Components allow developers to create a component once and use it multiple times throughout an Angular application.
Inputs such as @Input can be used to provide different data to each component instance, while @Output can be used to communicate events back to the parent.
Reusable components reduce duplicate code, improve consistency, simplify maintenance and make Angular applications easier to scale.