Reputation: 806
I'm using Angular 5, trying to load a empty-path child route into a empty-path parent layout route. The FullLayoutComponent always loads, and the WhyUsComponent component loads when I visit localhost:4200/why-us.
But I cannot get the FrontpageComponent to load when I visit localhost:4200
If I change the path for FrontPageComponent to front-page, it will load when I visit localhost:4200/front-page.
It seems like empty child path inside empty parent path doesn't work (i've tried all combinations of pathMatch btw)
I need to the FrontpageComponent to load at the root of my site, without any defined path.
RouterModule.forRoot([
{
path: '',
component: FullLayoutComponent,
children: [
{
path: '',
component: FrontpageComponent,
pathMatch: 'full',
data: {
meta: {
title: '',
description: ''
}
}
},
{
path: 'why-us', component: WhyUsComponent, pathMatch: 'full',
data: {
meta: {
title: 'Why Us?',
description: 'Why would you choose us? '
}
}
}] // close children
}
])
Upvotes: 2
Views: 2752
Reputation: 806
I managed to find a way that works, lazy loading
RouterModule.forRoot([
{
path: '',
component: FullLayoutComponent,
loadChildren: 'path/to/my.module#MyModule'
}
and in MyModule, I have my routes defined, including the root path with an empty string.
Upvotes: 1
Reputation: 737
Try this:
{
path:'',
pathMatch: 'prefix',
//this will load your page in the router inside your main page
children:[
{
path: '',
component: FrontpageComponent,
pathMatch: 'full'
},
//This will load your main component
{
path: '',
component: FullLayoutComponent
},
{
path: 'why-us',
component: WhyUsComponent,
pathMatch: 'full'
}
]
}
And this code below your routes in your routing module:
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [ {
provide: LocationStrategy,
useClass: PathLocationStrategy} ]
})
Upvotes: 0
Reputation: 242
Will the below work?
RouterModule.forRoot([
{
path: '',
component: FullLayoutComponent,
children: [
{
path: '',
redirectTo: 'front-page'
component: FrontpageComponent,
pathMatch: 'full',
data: {
meta: {
title: '',
description: ''
}
}
},
{
path: 'front-page',
component: FrontpageComponent,
pathMatch: 'full',
data: {
meta: {
title: '',
description: ''
}
}
},
{
path: 'why-us', component: WhyUsComponent, pathMatch: 'full',
data: {
meta: {
title: 'Why Us?',
description: 'Why would you choose us? '
}
}
}] // close children
}
])
Upvotes: 0