Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 bootstrap multi root component

Tags:

angular

I would like to be able to boot multiple app root component into my index.html page. This was initially possible on angular 2 beta 0 - 15, but starting in beta 16, it is not working anymore. Please refer to this code http://plnkr.co/edit/A7fyFUST9IdP1FriauXk?p=preview

please see plunker link above for the code

Hope somebody can modify the code to be able to work on angular 2 beta 17.

Thanks in advance.

like image 877
marvs Avatar asked May 06 '16 07:05

marvs


People also ask

Can we bootstrap multiple component in Angular?

You can manually bootstrap your app by using angular. bootstrap() function, for multiple modules.

Can we have multiple root components in Angular?

In such scenarios, an Angular Element or web component should be isolated, fulfil a single functionality and communicate with the outside world using inputs & outputs. You can however also compile multiple Angular Elements out of a single Angular app, use them separately inside some index.

Can we bootstrap multiple components?

The bootstrap arraylink Inserting a bootstrapped component usually triggers a cascade of component creations that fill out that tree. While you can put more than one component tree on a host web page, most applications have only one component tree and bootstrap a single root component.

What is bootstrapModule in Angular?

The bootstrap property or key of the NgModule decorator specifies which component should be loaded by Angular when the app-level module loads. Angular reads the bootstrap metadata and loads the app-level component, called AppComponent .


2 Answers

Just list all the components in bootstrap (and declarations)

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, ModalComponent, CompComponent],
  providers: [SharedService],
  entryComponents: [CompComponent],
  bootstrap: [ App, ModalComponent ]
})
export class AppModule{}

See also How to dynamically create bootstrap modals as Angular2 components? for a full example.

like image 140
Günter Zöchbauer Avatar answered Oct 21 '22 11:10

Günter Zöchbauer


http://plnkr.co/edit/APFwcpOGsMlOMVdGCkCI

It got much easier with newer versions:

bootstrap(Comp1);
bootstrap(Comp2);

Also here is a more advanced example of how to implement communication between applications: http://plnkr.co/edit/aZqdJe3OZ8K2odHioWkB

like image 36
pajics Avatar answered Oct 21 '22 10:10

pajics