Function: useInjection()
Call Signature
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, token: ServiceToken<T>): UseInjectionValue<T>;Defined in: src/wirestate-lit/consumption/use-injection.ts:102
Consumes a value from the nearest Lit container context.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
host | ReactiveControllerHost & HTMLElement | Host element. |
token | ServiceToken<T> | Token to inject, or options carrying the token plus value/optional/fallback. |
Returns
Injection holder.
Remarks
Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.
Example
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });Call Signature
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, options: UseInjectionOptions<T, undefined> & {
fallback?: undefined;
optional?: false;
}): UseInjectionValue<T>;Defined in: src/wirestate-lit/consumption/use-injection.ts:106
Consumes a value from the nearest Lit container context.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
host | ReactiveControllerHost & HTMLElement | Host element. |
options | UseInjectionOptions<T, undefined> & { fallback?: undefined; optional?: false; } | - |
Returns
Injection holder.
Remarks
Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.
Example
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });Call Signature
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, options: UseInjectionOptions<T, undefined> & {
fallback?: undefined;
optional: true;
}): UseInjectionValue<T, undefined>;Defined in: src/wirestate-lit/consumption/use-injection.ts:110
Consumes a value from the nearest Lit container context.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
host | ReactiveControllerHost & HTMLElement | Host element. |
options | UseInjectionOptions<T, undefined> & { fallback?: undefined; optional: true; } | - |
Returns
UseInjectionValue<T, undefined>
Injection holder.
Remarks
Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.
Example
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });Call Signature
function useInjection<T, F>(host: ReactiveControllerHost & HTMLElement, options: UseInjectionOptions<T, F> & {
fallback: InjectionFallback<F>;
}): UseInjectionValue<T, F>;Defined in: src/wirestate-lit/consumption/use-injection.ts:114
Consumes a value from the nearest Lit container context.
Type Parameters
| Type Parameter |
|---|
T |
F |
Parameters
| Parameter | Type | Description |
|---|---|---|
host | ReactiveControllerHost & HTMLElement | Host element. |
options | UseInjectionOptions<T, F> & { fallback: InjectionFallback<F>; } | - |
Returns
UseInjectionValue<T, F>
Injection holder.
Remarks
Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.
Example
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });