Reusable Components & Architecture in Angular
Building reusable components and following a scalable architecture is essential for maintaining large Angular applications efficiently.
1. Smart vs Dumb Components
Smart Components (Container Components)
- Handle business logic and data fetching
- Interact with services and state
- Pass data to dumb components via
@Inputand receive events via@Output
@Component({
selector: 'app-user-container',
template: `<app-user-list [users]="users" (delete)="deleteUser($event)"></app-user-list>`
})
export class UserContainerComponent {
users = [];
constructor(private userService: UserService) {}
ngOnInit() {
this.users = this.userService.getUsers();
}
deleteUser(userId: number) {
this.userService.deleteUser(userId);
}
}
Dumb Components (Presentational Components)
- Focus on UI rendering only
- Receive data via
@Inputand emit events via@Output - No direct service or state dependency
@Component({
selector: 'app-user-list',
template: `
<ul>
<li *ngFor="let user of users">
{{ user.name }}
<button (click)="delete.emit(user.id)">Delete</button>
</li>
</ul>
`
})
export class UserListComponent {
@Input() users: any[] = [];
@Output() delete = new EventEmitter<number>();
}
2. Shared Components
Components that are reused across multiple modules:
- Buttons, modals, tables, input fields
- Place in a shared module and import in other modules:
@NgModule({
declarations: [ButtonComponent, ModalComponent],
exports: [ButtonComponent, ModalComponent]
})
export class SharedModule {}
3. Feature Folders
Organize code by features rather than by type:
/users
/components
user-list.component.ts
user-form.component.ts
/services
user.service.ts
/models
user.model.ts
user.module.ts
- Makes it easy to maintain, scale, and lazy-load modules
4. Clean & Scalable Architecture
Best Practices:
- Smart/Dumb separation – keeps UI and logic separate
- Feature modules – organize by domain (users, products, orders)
- Shared module – reusable components, directives, pipes
- Core module – singleton services and app-wide providers
- State management – RxJS or NgRx for predictable state
- Lazy loading – improve performance for large apps
- Follow Angular style guide for consistent folder structure
Summary
- Separate smart (container) and dumb (presentational) components.
- Use shared components for reuse across modules.
- Organize code into feature folders for scalability.
- Maintain a clean and modular architecture for maintainability and performance.