Description
Use standalone components instead of NgModules when possible to improve modularity, reduce boilerplate, and enable better tree-shaking.
When to use:
- New components - Always prefer standalone for new components unless there’s a specific need for NgModules
- Leaf components - Components that don’t need to declare other components
- Reusable components - Components intended for use across different parts of the application
- Library components - When building Angular libraries for better consumer experience
Benefits:
- Reduced Boilerplate - No need to create and maintain NgModule files
- Better Tree-shaking - Unused dependencies are more easily eliminated
- Improved Modularity - Each component manages its own dependencies
- Simpler Testing - Easier to set up component tests without module configuration
- Faster Development - Less configuration needed when creating new components
When NOT to use:
- Legacy applications where migration cost is too high
- When you need to provide global services at the module level
- Complex applications with deeply nested module hierarchies that serve a specific architectural purpose
- When working with third-party libraries that require NgModule integration