Skip to content

Function: useInjection()

ts
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, optionsOrInjectionId: 
  | UseInjectionOptions<T>
| ServiceIdentifier<T>): UseInjectionValue<T>;

Defined in: src/wirestate-lit/consumption/use-injection.ts:82

Consumes a service from the nearest Lit container context.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
hostReactiveControllerHost & HTMLElementHost element.
optionsOrInjectionId| UseInjectionOptions<T> | ServiceIdentifier<T>Service token or options.

Returns

UseInjectionValue<T>

Mutable injection holder.

Examples

typescript
class MyElement extends LitElement {
  private myService = useInjection(this, MyService);

  render() {
    return html`<div>${this.myService.value.getName()}</div>`;
  }
}
typescript
class MyElement extends LitElement {
  private myService = useInjection(this, { injectionId: MyService, once: true });

  render() {
    return html`<div>${this.myService.value.getName()}</div>`;
  }
}