@Injectable
Marks a class as available to Angular's dependency injection system.
Syntax
@Injectable(config?: { providedIn }) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
config | object | No | Optional; providedIn: 'root' registers a tree-shakable singleton. |
Returns
ClassDecorator — Decorator enabling DI for the class.
Examples
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class UserService {
getUsers() {
return fetch('/api/users').then((r) => r.json());
}
}
Notes
`providedIn: 'root'` creates an application-wide singleton that is
tree-shakable when unused. The decorator is also required whenever a class
has injected dependencies of its own. Inject the service via the inject()
function or constructor parameters.