I have the following module structure:
1- RootModule
With routing as follows:
const routes: Routes = [
{ path: '', redirectTo: 'app', pathMatch: 'full' }
];
2- AppModule
With routing as follows:
const routes: Routes = [
{
path: 'app',
component: AppComponent,
children: [
{
path: '',
redirectTo: 'index',
pathMatch: 'full'
}
]
}
];
Also, AppModule
imports MainModule
which is only a routing module and is configured as follows:
const routes: Routes = [
{
path: '',
component: MainComponent,
children: [
{
path: 'index',
loadChildren: '.\/..\/modules\/index\/index.module#IndexModule'
},
{
path: '',
redirectTo: 'index',
pathMatch: 'full'
}
]
}
];
Now, RootComponent
is the starting point:
@Component({
selector: "app-root",
template: "<router-outlet></router-outlet>"
})
export class RootComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
The AppComponent
is defined as:
<router-outlet></router-outlet>
<app-quick-sidebar></app-quick-sidebar>
Finally, MainComponent
is defined as:
<app-header-nav></app-header-nav>
<router-outlet></router-outlet>
<app-footer></app-footer>
The whole point is to route the application to /index component in such a way to pass through RooComponent
--> AppComponent
--> MainComponent
--> IndexComponent
So far, with the above routes, AppComponent
is bypassed!
Any idea?
Thanks
Nested routes are routes within other routes. In this tutorial, we will show you how to create a child route and display the child components. The Angular allows us to nest child routes under another child routes effectively creating a Tree of routes.
To recap, nested routes allow you to, at the route level, have a parent component control the rendering of a child component. Twitter's /messages route is the perfect example of this. With React Router, you have two options for creating nested routes.
You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows.
With your current routes configuration, MainComponent
is not configured in the children array of AppComponent
's path. So why would it appear in its template?
Right now you routing configuration will work like this:
/app
will get you to AppComponent
/index
will get you to the IndexComponent
.To achieve the desired behavior of RooComponent
--> AppComponent
--> MainComponent
--> IndexComponent
, your routes configuration should look like this:
const routes: Routes = [{
path: '',
component: AppComponent,
children: [{
path: '',
component: MainComponent,
children: [{
path: '', redirectTo: 'index', pathMatch: 'full'
}, {
path: 'index',
loadChildren: '.\/..\/modules\/index\/index.module#IndexModule'
}]
}]
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With