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 Parameter | Default type | Description |
|---|---|---|
T | - | The type of the value being resolved. |
F | null | The type returned by the fallback function. |
Parameters
| Parameter | Type | Description |
|---|---|---|
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;
}