Global

Methods

# inject(categoryopt, transformeropt) → {function}

Allows for the injection of an injectable decorated dependency into a class property. The property must be typed for the requested dependency. Only concrete classes are supported; generics are not.

Injected properties should be described like so:

    class ClassName {
        ...

Property decorator that injects a dependency into a class property.

sequenceDiagram participant Client participant Decorator participant Injectables Client->>Decorator: @inject() Decorator->>Decorator: Get type from property Decorator->>Decorator: Define metadata Decorator->>Decorator: Define property getter Note over Decorator: When property accessed Client->>Decorator: access property Decorator->>Decorator: Check if instance exists alt Instance exists in WeakMap Decorator-->>Client: Return cached instance else No instance Decorator->>Injectables: get(name) alt Injectable found Injectables-->>Decorator: Return injectable instance opt Has transformer Decorator->>Decorator: Call transformer end Decorator->>Decorator: Store in WeakMap Decorator-->>Client: Return instance else No injectable Decorator-->>Client: Throw error end end
Parameters:
Name Type Attributes Description
category string <optional>

Defaults to the class name derived from the property type. Useful when minification occurs and names are changed, or when you want to upcast the object to a different type.

transformer InstanceTransformer <optional>

Optional function to transform the injectable instance before it's injected.

View Source decorators.ts, line 55

A property decorator function that sets up the dependency injection.

function

# injectable(categoryopt, forceopt, instanceCallbackopt) → {function}

Defines a class as an injectable singleton that can be retrieved from the registry. When applied to a class, replaces its constructor with one that returns a singleton instance.

Decorator that marks a class as available for dependency injection.

sequenceDiagram participant Client participant Decorator participant Injectables Client->>Decorator: @injectable() Decorator->>Decorator: Create new constructor Note over Decorator: When new instance requested Decorator->>Injectables: get(name) alt Instance exists Injectables-->>Decorator: Return existing instance else No instance Decorator->>Injectables: register(original, name) Decorator->>Injectables: get(name) Injectables-->>Decorator: Return new instance opt Has callback Decorator->>Decorator: Call instanceCallback end end Decorator->>Decorator: Define metadata Decorator-->>Client: Return instance
Parameters:
Name Type Attributes Description
category string <optional>

Defaults to the class name. Useful when minification occurs and names are changed, or when you want to upcast the object to a different type.

force boolean <optional>

Defines if the injectable should override an already existing instance (if any). Only meant for extending decorators.

instanceCallback function <optional>

Optional callback function that will be called with the instance after creation.

View Source decorators.ts, line 162

A decorator function that transforms the class into an injectable.

function