Skip to content

Function: optionalInjection()

ts
function optionalInjection<T, F>(optionsOrInjectionId: 
  | OptionalInjectionOptions<T, F>
| ServiceIdentifier<T>, onFallback?: OptionalInjectionFallback<F>): OptionalInjectionDecorator<T, F>;

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

Injects a container value if it exists.

Type Parameters

Type ParameterDefault typeDescription
T-The type of the value being resolved.
FnullThe type returned by the fallback function.

Parameters

ParameterTypeDescription
optionsOrInjectionId| OptionalInjectionOptions<T, F> | ServiceIdentifier<T>Service token or options.
onFallback?OptionalInjectionFallback<F>Fallback for missing bindings.

Returns

OptionalInjectionDecorator<T, F>

Lit property decorator.

Remarks

Missing token means fallback result, or null when no fallback exists.

Example

typescript
class MyElement extends LitElement {
  @optionalInjection(FileLogger, (container) => container.get(ConsoleLoggerService))
  private logger: Logger | null = null;
}