What I'm trying to accomplish:
I have multiple nested feature modules.
AppModule
-- DashboardModule
----- DashboardReportsModule
On path 'dashboard', I want to redirect the user to 'dashboard/reports' (the main view of the dashboard), while loading a DashboardComponent from DashboardModule which contains some layout html, other shared components & another router-outlet element (with name="dashboard-main-outlet").
The second router-outlet should then load the DashboardReportsComponent from DashboardReportsModule (which will contain some other components & html...).
*The second router-outlet should be the one to change when navigating to other inner paths (e.g. 'dashboard/settings','dashboard/...') while keeping the DashboardComponent layout in place.
How I tried to do that:
From the root app, on path named 'dashboard' I'm routing to DashboardModule like that (PageNotFoundComponent is a shared 404 component):
src/app/app-routing.module
...
{
path: 'dashboard',
loadChildren: './dashboard/dashboard.module#DashboardModule'
},
...
Then, on DashboardRoutingModule, I added this:
src/app/dashboard/dashboard-routing.module.ts
...
{
path: '',
pathMatch: 'full',
component: DashboardComponent,
redirectTo: 'reports'
},
{
path: '',
children: [
{
path: 'reports',
loadChildren: './dashboard-reports/dashboard-reports.module#DashboardReportsModule',
outlet: 'dashboard-main-outlet'
}
]
},
{ path: '**', component: PageNotFoundComponent }
...
This is the template of DashboardComponent (where the second router-outlet lives, the main one is placed in the AppComponent template):
src/app/dashboard/dashboard.component.html
<h1 class="title">Welcome to the Dashboard!</h1>
<router-outlet name="dashboard-main-outlet"></router-outlet>
The result:
When browsing to 'dashboard' I'm redirected to 'dashboard/reports' & get the 404 message.
On the other hand, when I remove the outlet: 'dashboard-main-outlet'
, I see the content of the DashboardReportsComponent, but without the <h1>
from the DashboardComponent (It's loaded into the main router-outlet).
What am I doing wrong?
Any suggestion would do.