* Add a component to this application and register extensions such as * controllers, providers, and servers from the component. * * @param componentCtor - The component class to add. * @param nameOrOptions - Optional component name or options, default to the * class name * * @ex
(
componentCtor: Constructor<T>,
nameOrOptions?: string | BindingFromClassOptions,
)
| 460 | * ``` |
| 461 | */ |
| 462 | public component<T extends Component = Component>( |
| 463 | componentCtor: Constructor<T>, |
| 464 | nameOrOptions?: string | BindingFromClassOptions, |
| 465 | ) { |
| 466 | this.debug('Adding component: %s', nameOrOptions ?? componentCtor.name); |
| 467 | const binding = createBindingFromClass(componentCtor, { |
| 468 | namespace: CoreBindings.COMPONENTS, |
| 469 | type: CoreTags.COMPONENT, |
| 470 | defaultScope: BindingScope.SINGLETON, |
| 471 | ...toOptions(nameOrOptions), |
| 472 | }); |
| 473 | // Check if the component is already bound |
| 474 | const found = this.registry.get(binding.key); |
| 475 | if (found?.valueConstructor === binding.valueConstructor) { |
| 476 | return binding; |
| 477 | } |
| 478 | if (isLifeCycleObserverClass(componentCtor)) { |
| 479 | binding.apply(asLifeCycleObserver); |
| 480 | } |
| 481 | this.add(binding); |
| 482 | // Assuming components can be synchronously instantiated |
| 483 | const instance = this.getSync<Component>(binding.key); |
| 484 | mountComponent(this, instance); |
| 485 | return binding; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Set application metadata. `@loopback/boot` calls this method to populate |