Function subcomponent

  • Defines a subcomponent that applies the scopes and dependencies provided in setup to a new child container.

    Type Parameters

    • Args extends any[]

      The arguments that the resolved Subcomponent will accept to produce the child container.

    • G extends ProvideGraph

    Parameters

    • setup: ((ct, ...args) => Container<G>)

      A function that adds Scopes and provides dependencies to a new child container.

    Returns SubcomponentDefinition<Args, G>

    A SubcomponentDefinition producing a Subcomponent that transforms Args into a container with the dependencies and scopes provided by setup

    Example

    Defining a subcomponent:

    const UserSubcomponent = Inject.subcomponent(
    (ct, name: string, id: number) => ct
    .addScope(UserScope)
    .provideInstance(NameKey, name)
    .provideInstance(IdKey, id)
    )

    Example

    Using a subcomponent:

    Requesting and calling the subcomponent:

    const child = parent.request(UserSubcomponent)('Alice', 123)
    

    build:

    const child = parent.build(UserSubcomponent, 'Alice', 123)
    

    Build:

    parent.inject({ child: UserSubcomponent.Build('Alice', 123) }, ({ child }) => {
    // ...
    })

Generated using TypeDoc