Closed
Description
[prefer-inject] Prefer injecting dependencies via inject instead of constructor arguments
https://angular.dev/guide/di/dependency-injection#injecting-consuming-a-dependency
The most common way to inject a dependency is to declare it in a class constructor. When Angular creates a new instance of a component, directive, or pipe class, it determines which services or other dependencies that class needs by looking at the constructor parameter types. For example, if the
HeroListComponent
needs theHeroService
, the constructor can look like this:@Component({ … }) class HeroListComponent { constructor(private service: HeroService) {} }Another option is to use the inject method:
@Component({ … }) class HeroListComponent { private service = inject(HeroService); }
I'd love to be able to prefer the inject(HeroService)
format, with auto-fixer