Function: Inject()
function Inject(ctor: Constructor): (_: undefined, ctx: ClassFieldDecoratorContext) => void;
Defined in: src/DI/decorators/inject.ts:39
Decorator to inject a service dependency from the global DI container
Works by creating a lazy getter that retrieves the service from the container associated with the instance. The value is cached after first access.
Parameters
| Parameter | Type | Description |
|---|---|---|
ctor |
Constructor |
Constructor of the service to inject |
Returns
(_: undefined, ctx: ClassFieldDecoratorContext): void;
Parameters
| Parameter | Type |
|---|---|
_ |
undefined |
ctx |
ClassFieldDecoratorContext |
Returns
void
Examples
@Service
class AuthService {
@Inject(HttpClient) http!: HttpClient;
@Inject(ConfigService) config!: ConfigService;
async login(credentials: Credentials) {
const url = this.config.getApiUrl();
return this.http.post(url, credentials);
}
}
@Component
class MyComponent extends BaseComponent {
@Inject(Router) router!: Router;
@Inject(AuthService) auth!: AuthService;
view() {
return <div>Usuario: {this.auth.currentUser}</div>;
}
}