core / Function

injectRef

Generic types:T

Inject a proxy of the target dependency, which defers the actual injection until any of its properties is accessed.

Notes

This allows circular dependency injection (even though you ought not to), as long as the two classes do not access each other's properties during construction.

Presentation

function injectRef(
  token: ProviderToken<T>,
  injector: Injector = inject(Injector),
): T;

Returns

T

Parameters

NameTypeDescription
token
ProviderToken<T>
injector
Injector

Example usage

export class MyService {
  private dep = injectRef(AnotherService);
}
export class AnotherService {
  private dep = injectRef(MyService);
}