The inject Function
Resolve dependencies with the inject function.
The inject Function
The inject() function retrieves a dependency from the injector. It is the modern alternative to constructor parameter injection.
Basic Usage
Call inject with the class you want, and assign the result to a field.
import { Component, inject } from '@angular/core';
import { DataService } from './data.service';
@Component({ selector: 'app-x', template: '' })
export class XComponent {
private data = inject(DataService);
}All lessons in this course
- Services and Injectable
- The inject Function
- Providers and Scopes
- Injection Tokens