3

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.

Edit

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jose9988
  • 65
  • 1
  • 2
  • 11
  • 1
    not like I see the issue, just want to help (: those 2 paths: `{path: '', pathMatch: 'full', component: HomeComponent}` and `{path: '', component: HomeComponent}` seems like a duplicate. I suppose just one `{path: '', component: HomeComponent}` should be enough – IAfanasov May 26 '20 at 07:55
  • Also, you can try to record with the chrome dev tool performance tab the code which is executed on click and explore it. that is a long shot thought. – IAfanasov May 26 '20 at 07:57
  • 2
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 26 '20 at 11:40
  • Can you try this? const routes: Routes = [{path: 'about', component: AboutUsComponent}]; AND loadChildren for AboutModule in app router – Marc May 26 '20 at 11:51
  • @Marc , what do you mean by: "loadChildren for AboutModule in app router". I already have the loadChildren route in my app-routing.module.ts – Jose9988 May 26 '20 at 13:30
  • You're right. Please remove the first two lines in routing: delete: {path: '', pathMatch: 'full', component: HomeComponent}, // no route specified {path: '', component: HomeComponent}, – Marc May 26 '20 at 14:55

2 Answers2

4

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

Marc
  • 1,836
  • 1
  • 10
  • 14
  • After removing a repeated `BrowserModule` the route debugging gives me: `RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.)`. – Jose9988 May 26 '20 at 15:45
  • in one of your sub router modules is a RouterModule.forRoot(...), I think – Marc May 26 '20 at 15:53
  • There is only one RouterModule.forRoot and it is placed in the app-routing.module.ts. It seems like it is being imported twice – Jose9988 May 26 '20 at 15:59
  • I mean, I have one AppRoutingModule in my shared module, but I can not remove, because otherwise, it will give an error – Jose9988 May 26 '20 at 16:03
  • Why ? AppRoutingModule should used in AppModule only. – Marc May 26 '20 at 16:10
  • Ow ok, my modules should import from appmodule or the sharedmodule? – Jose9988 May 26 '20 at 16:17
  • Import BrowserModule, BrowserAnimationsModule and AppRoutingModule in AppModule. Import all other Modules where it's used. Import AppRoutingModule only once (in AppModule). Im not a friend of a big SharedModule . – Marc May 26 '20 at 16:22
  • Okay, after using sharedmodule in the child routes, now I am able to use the routes if I type the route directly into the browser but, the routerLink link isn't working now – Jose9988 May 26 '20 at 16:28
  • " the routerLink link isn't working now?" means what? tracing? – Marc May 26 '20 at 16:31
  • I mean that 0 errors are showing, even with the route tracing. When I hover the mouse to the navbar element that will redirect me to the `about` route, it shows the default cursor and not the pointer one. But if I click to one link using href or I manually go to the route it works. So i maybe it has to do something with the `RouterModule` import. – Jose9988 May 26 '20 at 16:34
  • I had to add the `RouterModule` to the `SharedModule` and use the `SharedModule` in all my other modules and lazy components such as `AboutUsModule`. So it is working now. Thanks! – Jose9988 May 26 '20 at 16:45
0

Disabling Ivy option and check. it's working with me

"enableIvy": false,

Fahd Allebdi
  • 354
  • 4
  • 7