I'm trying to implement lazy routing into my app.
I have a very big project and when it was at router-deprecated I used AsyncRoute, but now it was removed.
So I tried to implement newest lazy loading, but I got an issue RangeError: Maximum call stack size exceeded What I'm doing wrong? I did all code like in instructions.
Take a look please
EncountersModule
import { NgModule } from '@angular/core';
// import { CommonModule } from '@angular/common';
/* --------------- !System modules --------------- */
import { SharedModule } from 'sharedModule'; //There is a lot of shared components/directives/pipes (over 60) and it re-exports CommonModule so I can't avoid it
/* --------------- !App outer modules --------------- */
import { EncountersComponent } from './encounters.component';
// import { PassCodeComponent } from '../../shared/components/passcode/passcode.component';
@NgModule({
imports: [ SharedModule ],
declarations: [ EncountersComponent],
exports: [ EncountersComponent ],
})
export class EncountersModule { }
Here is my app.routing.module
import { NgModule } from '@angular/core';
// import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ImagingComponent } from '../modules/index';
import { DashboardComponent } from '../modules/index';
import { PrescriptionNoticesComponent } from '../modules/index';
// import { EncountersComponent } from "../modules/encounters/encounters.component";
import { ScheduleComponent } from "../modules/schedule/schedule.component";
import { AdminComponent } from '../modules/index';
@NgModule({
imports: [
RouterModule.forRoot([
{
path: '',
component: DashboardComponent,
data: { label: 'Dashboard' }
},
{
path: 'encounters',
// component: EncountersComponent,
loadChildren: 'production/modules/encounters/encounters.module#EncountersModule',
data: { label: 'Encounters' }
},
{
path: 'admin',
component: AdminComponent,
data: { label: 'Admin' }
}
])
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
// const appRoutes: Routes = [
// {
// path: 'imaging',
// component: ImagingComponent,
// data: { label: 'Imaging' }
// },
// {
// path: '',
// component: DashboardComponent,
// data: { label: 'Dashboard' }
// },
// {
// path: 'prescription_notices',
// component: PrescriptionNoticesComponent,
// data: { label: 'Prescription Notices' }
// },
// {
// path: 'encounters',
// component: EncountersComponent,
// data: { label: 'Encounters' }
// },
// {
// path: 'schedule',
// component: ScheduleComponent,
// data: { label: 'Schedule' }
// },
// {
// path: 'admin',
// component: AdminComponent,
// data: { label: 'Admin' }
// }
// ];
//
// export const appRoutingProviders: any[] = [
//
// ];
//
// export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
I was facing same issue and I had everything correct. The following worked for me
ng serve
It started working again spontaneously. First make sure you have everything correct as mentioned in other answers and then try this.
Try removing comments. When I updated my router to current in the application I was working on I commented a bunch of stuff out from the old router because I didn't want to lose it. After removing comments some of the strange errors went away.
loadChildren needs to reference module with routing
By assigning a value to loadChildren property inside a route, you have to reference a module, which has a routing system implemented. In other words reference only a module that imports RoutingModule and configures it with forChild(routes) method.
I am not sure because it is not explicitly mentioned in the documentation (2.4.2), but from the examples in the "Angular Modules" and "Routing & Navigation" guides, I have induced the following pattern:
path
property of that element should be an empty string; thecomponent
property should be defined (necessary when the lazy module provides any service in order to injection works well) and the template of the referenced component should have an element with the<router-outlet>
directive. This route usually has achildren
property.path
property of the lazy route defined in the "app-routing.module" ("lazyModulePrefix" in my example) would be the prefix of all the paths defined in the ".lazy-routing.module".For example:
.
.
.
With the above code, the routes mapping would be:
http://host/login -> LoginComponent
http://host/lazyModulePrefix -> LazyModuleHomeComponent
http://host/lazyModulePrefix/somePath1 -> AComponentDeclaredInTheLazyModule1
http://host/lazyModulePrefix/somePath2 -> AComponentDeclaredInTheLazyModule2
http://host/anythingElse -> PageNotFoundComponent