Requirement:
- Use multiple Root-Components in the same Application
- This Root-Components use the same Implementations and Services
- Display the Root-Components on the same Single-Page
Solution:
App-Module preparation:
Angular 2 has two options to provide a service.
Replace the bootstrap declaration with: import { BrowserModule, DOCUMENT } from '@angular/platform-browser';
import { NgModule, Inject, Component } from '@angular/core';
export class AppModule {
private browser_document;
ngDoBootstrap(appRef){
if(this.browser_document.getElementsByTagName('app-root1').length > 0){appRef.bootstrap(AppComponent1);}
if(this.browser_document.getElementsByTagName('app-root2').length > 0){appRef.bootstrap(AppComponent2);}
}
constructor(@Inject(DOCUMENT) private document: any){
this.browser_document = document;
}
}
Service preparation:
Angular 2 has two options to provide a service.
1. Single instance
For a multiple implementation of more Root-Components we need the second option.
2. Multiple instance
Bootstrap: 2. Multiple instance
entryComponents: [
AppComponent1,
AppComponent2
]
Index.html: <html>
<head>
<title>TestApplication</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root1>Loading...</app-root1>
<app-root2> Loading...</app-root2>
</body>
</html>
In cooperation with Ludwig Fischer
Comments
Post a Comment