I've just upgraded to Angular 9 and created some child routes using lazy loading. Before these changes, my project and my routes worked perfectly but after these two changes, just my HomeComponent
route (path: "") works correctly, but if I try to click the links to other routes (using routerLink), it simply doesn't redirect, and if I manually write the route in the browser, it gives me a blank page without errors.
To sum up: The project and the routes worked fine, but after I created the angular lazy loading routes, the only route that works is the default one (HomeComponent) and if I try to click the links to go to other routes nothing happens, if I manually type these routes in my browser it gives me a blank page without no errors.
app.module.ts
@NgModule({
declarations: [
AppComponent,
HomeComponent,
],
imports: [
SharedModule,
HomeModule,
Mugan86GoogleAnalyticsModule.forRoot({analyticsId: 'XXX', showLog: false}),
AnimateOnScrollModule.forRoot(),
BrowserModule.withServerTransition({appId: 'serverApp'}),
ServiceWorkerModule.register('ngsw-worker.js', {enabled: environment.production}),
],
providers: [
{provide: ErrorHandler, useClass: RollbarErrorHandler},
{provide: RollbarService, useFactory: rollbarFactory}
],
bootstrap: [AppComponent],
exports: [
SharedModule,
HomeModule,
]
})
export class AppModule {
}
shared.module.ts
@NgModule({
declarations: [
FooterComponent,
LoadingBallsComponent,
NavbarComponent,
ScrolledToDirective,
TrackScrollDirective,
TopArrowComponent,
SnackbarComponent,
],
imports: [
BrowserModule,
RouterModule,
AppRoutingModule,
BrowserAnimationsModule,
CommonModule,
FilterModule,
ReactiveFormsModule,
FormsModule,
RecaptchaModule,
AnimateOnScrollModule
],
exports: [
BrowserModule,
CommonModule,
FooterComponent,
LoadingBallsComponent,
NavbarComponent,
FilterModule,
AppRoutingModule,
ReactiveFormsModule,
FormsModule,
RecaptchaModule,
AnimateOnScrollModule,
ScrolledToDirective,
TrackScrollDirective,
TopArrowComponent,
SnackbarComponent,
RouterModule,
BrowserAnimationsModule]
})
export class SharedModule {
}
home.module.ts
@NgModule({
declarations: [
CharacteristicsComponent,
ContactComponent,
HeaderComponent,
MainDescriptionComponent,
InfluencerPromotionComponent,
SocialMediaComponent,
BoxItemComponent,
BandIconComponent,
ServicesComponent,
],
imports: [
SharedModule,
],
exports: [
CharacteristicsComponent,
ContactComponent,
HeaderComponent,
MainDescriptionComponent,
InfluencerPromotionComponent,
SocialMediaComponent,
BoxItemComponent,
BandIconComponent,
ServicesComponent
]
})
export class HomeModule { }
app-routing.module.ts
export const routes: Routes = [
{path: '', pathMatch: 'full', component: HomeComponent}, // no route specified
{path: '', component: HomeComponent},
{path: 'sobre-nosotros', loadChildren: () => import('./components/about-us/about-us.module').then(m => m.AboutUsModule)},
{
path: 'preguntas-frecuentes',
loadChildren: () => import('./components/common-questions/common-questions.module').then(m => m.CommonQuestionsModule)
},
{path: 'sitemap', loadChildren: () => import('./components/sitemap/sitemap.module').then(m => m.SitemapModule)},
{path: '**', component: HomeComponent}, // fallback route (not found - 404)
];
@NgModule({
imports: [RouterModule.forRoot(routes, {initialNavigation: 'enabled', preloadingStrategy: PreloadAllModules})],
exports: [RouterModule]
})
export class AppRoutingModule {
}
As an example I will show the module and routing module of one of the routes that don't work:
about-us-module.ts
@NgModule({
declarations: [AboutUsComponent],
imports: [
AppModule,
CommonModule,
AboutUsRoutingModule,
]
})
export class AboutUsModule { }
about-us-routing.module.ts
const routes: Routes = [{path: '', component: AboutUsComponent}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AboutUsRoutingModule {
}
Extra information
tsconfig.json (angularCompilerOptions)
"angularCompilerOptions": {
"enableIvy": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
angular.json (aot)
"aot": true,
package.json (to see the versions)
"dependencies": {
"@angular/animations": "~9.1.9",
"@angular/common": "~9.1.9",
"@angular/compiler": "~9.1.9",
"@angular/core": "~9.1.9",
"@angular/forms": "~9.1.9",
"@angular/platform-browser": "~9.1.9",
"@angular/platform-browser-dynamic": "~9.1.9",
"@angular/platform-server": "~9.1.9",
"@angular/router": "~9.1.9",
"@angular/service-worker": "~9.1.9",
"@josee9988/filter-pipe-ngx": "^1.1.0",
"@nguniversal/express-engine": "^9.1.1",
"animate.css": "^3.7.2",
"emailjs-com": "^2.4.1",
"express": "^4.15.2",
"mugan86-ng-google-analytics": "^1.1.1",
"ng-recaptcha": "^5.0.0",
"ng2-animate-on-scroll": "^2.0.0",
"rollbar": "^2.16.2",
"rxjs": "~6.5.5",
"tslib": "^1.13.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.901.7",
"@angular/cli": "~9.1.7",
"@angular/compiler-cli": "~9.1.9",
"@angular/language-service": "~9.1.9",
"@nguniversal/builders": "^9.1.1",
"@types/express": "^4.17.0",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "^5.0.9",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.5.4",
"protractor": "^7.0.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.8.3"
I have another project using Ivy and child routing and I just followed the same steps I did with the mentioned project but I found the described problem.
I have just removed the lazy loading and the modules/routing modules of each route and the app works again. So definitely the problem is caused by the lazy loading feature.
Please remove the first two Home-Routes (HomeComponent).
In app-routing.module you can enable the route debugging with:
@NgModule({
imports: [
RouterModule.forRoot(routes, {enableTracing: true}),
Perhaps you can remove {initialNavigation: 'enabled', preloadingStrategy: PreloadAllModules}.
Your code looks fine. I hope that the debugging gives you more informations.
The AppRoutingModule should be used in AppModule only. It's not allowed to have a reference of AppRoutingModule in one of the lazy loaded sub modules.
Import BrowserModule, BrowserAnimationsModule and AppRoutingModule in AppModule.
Try to avoid big shared modules. See: Lazy Loading Angular - Code Splitting NgModules with Webpack