I totally agree with @pawan's answer, but yes @pawan I found a good solution to this. This solution definitely helped me and hopes that it will help all of you.
We do not need to create main.ts and AppComponent.ts for each page.
We need to make the main component that we load dynamically. In our case, in app.component.ts, I dynamically load our component based on the URL of the current page.
Say if your page is home.aspx, then boostrap HomeComponent or about.aspx, then boostrap AboutComponent I do this by implementing the ngDoBootstrap method as follows.
export class AppModule { url : string; constructor(@Inject(DOCUMENT) private document: any){ this.url = this.document.location.href.toLowerCase(); } ngDoBootstrap(app:ApplicationRef){ if(this.url.indexOf("home.aspx") > 0){ app.bootstrap(HomeComponent); } else if(this.url.indexOf("about.aspx") > 0){ app.bootstrap(AboutComponent); } } }
Here's how, based on the page URL, we can dynamically load our component and save a lot of main.ts and app.component.ts files for each page.
To do this, you need to add an entry for each component to the entryComponents array from the NgModule, as shown below:
@NgModule({ entryComponents : [ HomeComponent, AboutComponent, ] })
Hope this helps.
source share