Skip to main content

inject()

function inject\<T\>(token: InjectionToken\<T\>): T;

Defined in: src/lib/di/index.ts:132

Inject a service dependency.

Must be called during service construction (in field initializers or constructor). The service must be registered in a module that is a dependency of the current module.

On web runtime, if the service is not found, a no-op proxy is returned instead of throwing. This allows services to instantiate for @Web handler registration even when their dependencies (e.g., server-only repositories) aren't available.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokenInjectionToken<T>The service class or injection token to inject.

Returns

T

The service instance.

Throws

Error if called outside of injection context or if service not found (client/server only).

Example

import { inject } from "@core/di";
import { CharacterService } from "@modules/core/characters/character.service";
 
class MyService {
    private characterService = inject(CharacterService);
 
    doSomething() {
        return this.characterService.getActiveCharacter(1);
    }
}