What are @Injectable Decorators ?
@Injectable
is a decorator in Angular used to mark a class as an injectable that allows it to be provided and injected as a dependency within other Angular components, services, or modules.
Syntax:
@Injectable({
providedIn: 'root',
})
export class ServiceClass {
constructor() {}
}
Features of @Injectable decorators
- Singleton Pattern: Creates a single instance of a service to be shared across the application.
- Reusability: Services are designed to encapsulate certain capabilities, making integration and reuse simple.
- Lazy Loading: Optimizes performance by loading services only when needed, reducing initial load times.
Difference between @Injectable and @Inject Decorators
In Angular, decorators play an important role in defining and managing dependencies within the application. Two commonly used decorators are @Injectable
and @Inject
. While they both relate to dependency injection, they serve different purposes and are applied to different elements within an Angular application.
Table of Content
- What are @Injectable Decorators ?
- What are @Inject Decorators ?
- Difference between @Injectable and @Inject decorators
- Conclusion
Contact Us