Skip to content

Function: injection()

Call Signature

ts
function injection<T>(token: ServiceToken<T>): InjectionDecorator<T>;

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

Injects a container value into a Lit element property.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to inject, or options carrying the token plus optional/fallback.

Returns

InjectionDecorator<T>

Lit property decorator.

Remarks

Follows the nearest container context. Throws when the token is not bound. Pass optional to assign undefined on a miss, or a fallback (which implies optional) to assign a default.

Example

typescript
import { injection } from "@wirestate/lit";
import { LitElement } from "lit";

class MyElement extends LitElement {
  @injection(MyService)
  private service!: MyService;

  @injection({ token: MyService, optional: true })
  private maybeService?: MyService;

  @injection({ token: UserName, fallback: "guest" })
  private name!: string;
}

Call Signature

ts
function injection<T>(options: {
  fallback?: undefined;
  optional?: false;
  token: ServiceToken<T>;
}): InjectionDecorator<T>;

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

Injects a container value into a Lit element property.

Type Parameters

Type Parameter
T

Parameters

ParameterType
options{ fallback?: undefined; optional?: false; token: ServiceToken<T>; }
options.fallback?undefined
options.optional?false
options.tokenServiceToken<T>

Returns

InjectionDecorator<T>

Lit property decorator.

Remarks

Follows the nearest container context. Throws when the token is not bound. Pass optional to assign undefined on a miss, or a fallback (which implies optional) to assign a default.

Example

typescript
import { injection } from "@wirestate/lit";
import { LitElement } from "lit";

class MyElement extends LitElement {
  @injection(MyService)
  private service!: MyService;

  @injection({ token: MyService, optional: true })
  private maybeService?: MyService;

  @injection({ token: UserName, fallback: "guest" })
  private name!: string;
}

Call Signature

ts
function injection<T>(options: {
  fallback?: undefined;
  optional: true;
  token: ServiceToken<T>;
}): InjectionDecorator<Optional<T>>;

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

Injects a container value into a Lit element property.

Type Parameters

Type Parameter
T

Parameters

ParameterType
options{ fallback?: undefined; optional: true; token: ServiceToken<T>; }
options.fallback?undefined
options.optionaltrue
options.tokenServiceToken<T>

Returns

InjectionDecorator<Optional<T>>

Lit property decorator.

Remarks

Follows the nearest container context. Throws when the token is not bound. Pass optional to assign undefined on a miss, or a fallback (which implies optional) to assign a default.

Example

typescript
import { injection } from "@wirestate/lit";
import { LitElement } from "lit";

class MyElement extends LitElement {
  @injection(MyService)
  private service!: MyService;

  @injection({ token: MyService, optional: true })
  private maybeService?: MyService;

  @injection({ token: UserName, fallback: "guest" })
  private name!: string;
}

Call Signature

ts
function injection<T, F>(options: {
  fallback: InjectionFallback<F>;
  optional?: boolean;
  token: ServiceToken<T>;
}): InjectionDecorator<T | F>;

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

Injects a container value into a Lit element property.

Type Parameters

Type Parameter
T
F

Parameters

ParameterType
options{ fallback: InjectionFallback<F>; optional?: boolean; token: ServiceToken<T>; }
options.fallbackInjectionFallback<F>
options.optional?boolean
options.tokenServiceToken<T>

Returns

InjectionDecorator<T | F>

Lit property decorator.

Remarks

Follows the nearest container context. Throws when the token is not bound. Pass optional to assign undefined on a miss, or a fallback (which implies optional) to assign a default.

Example

typescript
import { injection } from "@wirestate/lit";
import { LitElement } from "lit";

class MyElement extends LitElement {
  @injection(MyService)
  private service!: MyService;

  @injection({ token: MyService, optional: true })
  private maybeService?: MyService;

  @injection({ token: UserName, fallback: "guest" })
  private name!: string;
}