Reputation: 35
I have a working app routing and i want to add lazy loading.
This ist the working app-routing.module.ts file (without lazy loading):
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './routes/login/login.component';
import { RegisterComponent } from './routes/register/register.component';
import { NavigationComponent } from './navigation/navigation.component';
import { DashboardComponent } from './routes/dashboard/dashboard.component';
import { ProfileComponent } from './routes/profile/profile.component';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{
path: 'app', component: NavigationComponent, children: [
{ path: '', component: DashboardComponent },
{ path: 'profile', component: ProfileComponent }
]
},
{ path: '**', redirectTo: 'app' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Now i created a child module for the "navigation" section (navigation.module.ts):
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NavigationComponent } from './navigation/navigation.component';
import { DashboardComponent } from './routes/dashboard/dashboard.component';
import { ProfileComponent } from './routes/profile/profile.component';
const routes: Routes = [
{ path: '', component: DashboardComponent },
{ path: 'profile', component: ProfileComponent },
];
@NgModule({
declarations: [
NavigationComponent,
DashboardComponent,
ProfileComponent,
],
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
],
bootstrap: [NavigationComponent]
})
export class NavigationModule { }
And this is the new app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './routes/login/login.component';
import { RegisterComponent } from './routes/register/register.component';
import { NavigationComponent } from './navigation/navigation.component';
import { DashboardComponent } from './routes/dashboard/dashboard.component';
import { ProfileComponent } from './routes/profile/profile.component';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{
path: 'app', loadChildren: () => import('./navigation/navigation.module').then(m => m.NavigationModule)
},
{ path: '**', redirectTo: 'app' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Now I have two different behaviours when I call for example /app/profile:
Behaviour without lazy loading (desired):
<app-root>
<router-outlet></router-outlet>
<app-navigation>
<!-- sidebar and toolbar content -->
<router-outlet></router-outlet>
<app-profile>
<!-- profile content -->
</app-profile>
</app-navigation>
</app-root>
Behaviour with lazy loading:
<app-root>
<router-outlet></router-outlet>
<app-profile>
<!-- profile content -->
</app-profile>
</app-root>
How is it possible to have the same behaviour with lazy loading?
Upvotes: 0
Views: 1842
Reputation: 5649
In NavigationModule try
const routes: Routes = [
{ path: '', component: NavigationComponent, children: [
{ path: '', component: DashboardComponent },
{ path: 'profile', component: ProfileComponent },
]
];
Upvotes: 2