ComponentHost
components.types.ts:154Defines an object that can host components.
Methods
addComponent(key: string, component: Component<THost>, options?: AddComponentOptions): Component<THost> Adds a new component to the host object. Throws if a component with the
specified key already exists. Calls onAdded() on the component once
registered with its host.
Parameters
-
keystring -
componentComponent<THost> -
options?AddComponentOptions
Returns
Component<THost> addComponentFromFactory(key: string, factory: ComponentFactory<THost>, options?: AddComponentOptions): Component<THost> Adds a new component to the host object using a factory function.
Internally produces the new component using the factory function, then
calls addComponent() with the result.
The advantage of using this method over addComponent() is that the
factory function is provided with the host.
Parameters
-
keystring -
factoryComponentFactory<THost> -
options?AddComponentOptions
Returns
Component<THost> addComponents(components: ComponentMap<THost>, options?: AddComponentOptions): ComponentMap<THost> Adds a new set of components to the host object. Throws if a component with
the specified key already exists. Calls onAdded() on each component
after they are all registered with the host, rather than one by one.
This is important for components that may want to reference each other
during the addition phase via host.getComponent() or similar methods.
It is recommended to use this method rather than addComponent() in
situations like initialization of a new host object.
Parameters
-
componentsComponentMap<THost> -
options?AddComponentOptions
Returns
ComponentMap<THost> addComponentsFromFactories(map: ComponentFactoryMap<THost>, options?: AddComponentOptions): ComponentMap<THost> Adds a new set of components to the host object based on an input map of
component keys to factory functions. Behavious is equivalent to
addComponents() using the key and output of each factory function.
Parameters
-
mapComponentFactoryMap<THost> -
options?AddComponentOptions
Returns
ComponentMap<THost> getComponent(key: string): T Gets a component from the host object using the key it was registered with.
Throws if the component does not exist. Performs an efficient lookup on a
local Map instance.
Parameters
-
keystring
Returns
T getComponentByType(type: ComponentHostConstructor<T>): T Gets a component from the host object using its type. Throws if no
component of the type exists, or if more than one component of the type
exists — in the multi-match case, getComponentByType deliberately
does not pick one for you. Use ComponentHost.getComponentsByType
when you genuinely expect multiple matches, or look the component up by
its string key.
Performs an O(n) lookup once per type initially, then caches the resolved key for O(1) lookups on subsequent calls. The cache is invalidated whenever a component is removed.
Parameters
-
typeComponentHostConstructor<T>
Returns
T getComponentsByType(type: ComponentHostConstructor<T>): readonly T[] Gets every component on the host of the given type, in the order they were originally registered. Returns an empty array if no components match.
Unlike ComponentHost.getComponentByType, this method never throws on multiplicity — it is the explicit "I expect more than one" accessor.
Parameters
-
typeComponentHostConstructor<T>
Returns
readonly T[] getNullableComponent(key: string): null | T Gets a component from the host object using the key it was registered with.
Returns null if the component does not exist, rather than throwing an
error. Useful for referencing transient or optional components without
manually handling errors.
Parameters
-
keystring
Returns
null | T getNullableComponentByType(type: ComponentHostConstructor<T>): null | T Gets a component from the host object using its type. Returns null if
the component does not exist or if more than one component of the type
is registered (i.e. the lookup is ambiguous) — the nullable variant
collapses both "not found" and "ambiguous" into a single null. Use
ComponentHost.getComponentsByType when you need to distinguish
them.
Parameters
-
typeComponentHostConstructor<T>
Returns
null | T hasComponent(key: string): boolean Checks if the host object has a component with the specified key.
Parameters
-
keystring
Returns
boolean hasComponentByType(type: ComponentHostConstructor<T>): boolean Checks if the host object has a component with the specified type.
Parameters
-
typeComponentHostConstructor<T>
Returns
boolean removeAllComponents(): void Removes all components from the host object. Typically called internally
when the lifecycle of the host object is terminated. Differs from
individually removing components in that it first calls onDestroy() on
each component, then removes references from the host object in a separate
step. This allows cleaner teardown of components that may reference each
other.
Returns
void removeComponent(key: string): void Removes a component from the host object. Care should be taken when
manually removing components, as methods like getComponent() will throw
if components do not exist. Removal is idempotent and will do nothing if
the component does not exist i.e. was already removed, or never existed.
Parameters
-
keystring
Returns
void