Why is the "bootstrap" key in @NgModule an array?

According to my knowledge, there can only be one entry point for an application. As shown in the below code snippet, we pass an array in the bootstrap key that defines the entry point of the application.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent, MyComboboxComponent, 
                    CollapsibleDirective, CustomCurrencyPipe],
  imports: [BrowserModule],
  providers: [UserService, LessonsService],
  bootstrap: [AppComponent]

})
export class AppModule {

}

      

PS: I am learning Angular 2 and the question may sound silly :)

+3


source to share


2 answers


You can transfer as many boot components as you like. You just end up with several independent components:

bootstrap: [AComponent, BComponent]

        RootModuleInjector
                |
                |
       ApplicationRef.views
       /                   \
      /                     \
   AComponent              BComponent

      

Also see What are the implications of self-tuning multiple components

When you run change detection, Angular will run change detection for each tree separately:

class ApplicationRef {
   tick(): void {
    ...
    try {
      this._runningTick = true;
      // here this._views.length equals to 2
      this._views.forEach((view) => view.detectChanges());

      

You can even add a new root component ApplicationRef

manually if you like:



const componentRef = componentFactoryResolver.resolveComponentFactory(SomeComponent)
applicationRef.attachView(componentRef.hostView);

        RootModuleInjector
                |
                |
       ApplicationRef.views
       /        |           \
      /         |            \
AComponent  SomeComponent   BComponent

      

If you need to share some data between root components, you can define the provider of the root module that will be used to create the RootModuleInjector:

@NgModule({
    providers: [ServiceSharedBetweenRootComponents]
}
export class AppModule {}

      

And then you can inject it into each root component:

export class AComponent {
    constructor(service: ServiceSharedBetweenRootComponents)

      

+5


source


there can be only one application entry point

Not. You can create multiple components as entry points in your application.

Example: fooobar.com/questions/229923 / ...



Here is an example of how we can communicate between root components

+3


source







All Articles