I have an Angular 5 application which can dynamically load feature modules and display their content in the main router outlet. The code below show my basic configuration:
app.component from root module (the main router outlet):
<router-outlet></router-outlet>
routing config for root (modules are loaded dynamically from passed urls):
const routes: Routes = [
{ path: 'module1', loadChildren: ...},
{ path: 'module2', loadChildren: ...}
];
export const AppRoutes = RouterModule.forRoot(routes);
example routing for one of the feature modules:
const routes: Routes = [
{ path: 'report/:reportId', component: ReportComponent}
];
export const AppRoutes = RouterModule.forChild(routes);
and its app.component which contains another router outlet:
<router-outlet></router-outlet>
Such configuration works correctly - if I go to mysite.com/module1, then module1 is loaded and angular gives control to the feature module router, so accessing mysite.com/module1/report/2 displays ReportComponent inside the feature module's router outlet.
What I'm trying to do now is to add TabsComponent to my main module to be able to compose pages from several tabs where each tab will contain one specific module. I would like to somehow use the already specified routing for root, without manually repeating it as children for the TabsComponent, and load the feature module into the TabsComponent's router outlet.
tabs.component:
<tabs-navigation></tabs-navigation>
<router-outlet></router-outlet>
tabs.configuration (loaded dynamically in runtime via http):
{
pageId: "page1",
tabs: [{
tabId: "tab1",
module: "module1",
params: "report/2"
}, {
tabId: "tab2",
module: "module2"
}]
}
And additional line in my routing config for root:
{ path: 'tabs/:pageId', component: TabsComponent}
From now on when accessing mysite.com/tabs/page1 I would like to:
- Read configuration for page1 via http endpoint.
- Generate links to all the configured tabs for that page as tabs navigation.
- Display tab's content inside the TabsComponent's router outlet after clicking a link (by default the first tab should be displayed). It basically means loading a feature module to that router outlet instead of the main one.
My two questions are:
- How to properly configure the routing provided I'd like to have all feature modules to be loaded either as a standalone page (as they work now) or as a tab in a tabbed page.
- How to generate links for the tabbed scenario so that I can go to the specific tab via navigation or directly from a given url? I guess I would need to include both page and tab id and the module details to make all the parts work correctly, e.g. mysite.com/page1/tab1/module1/report/2.