Reputation: 6875
I have a dashboard
module in my angular application. And my app.module.ts
is like following.
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
AppRoutingModule,
DashboardModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
And app-routing.module.ts
is
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
And dashboard.route.ts
is
const dashboardRoutes: Routes = [
{ path: 'dashboard', component: DashboardComponent }
]
@NgModule({
imports: [RouterModule.forChild(dashboardRoutes)],
exports: [RouterModule]
})
export class DashboardRouteModuel {}
But the applciaiton is routing to PageNotFoundComponent
always.
Upvotes: 0
Views: 48
Reputation: 433
Change order of imports AppRoutingModule and DashboardModule like following.
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
DashboardModule,
AppRoutingModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Upvotes: 0
Reputation: 46
Your routing concepts are not correct. Simply put, you have to do:
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', loadChildren: 'path/to/dashboard.module#DashboardModule' },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
const routes: Routes = [
{ path: '', component: DashboardComponent }
]
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class DashboardRouteModule {}
Upvotes: 1
Reputation: 32
You need to import the module routing on lazy loading. You can change:
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' }
by
{
path: '',
loadChildren:
'../../dashboard.module#DashboardModule'
}
Docs: Lazy loading
Upvotes: 0