question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lazy loading not working for nested lazy routes

See original GitHub issue

Note that at the moment lazy loading doesn’t work for nested routes. If you have route like lazy/lazy_child if you navigate first to lazy and then to lazy_child everything will work. However if you go directly to lazy/lazy_child Router will report unknown route error, since child routes will not be translated before Route matching mechanism kicks in.

Steps to reproduce

  • Create a lazy loaded module with nested routes e.g.
// main routes
{ path: 'lazy', loadChildren: 'path/to/some/route/lazy.module#LazyModule' }

// routes in LazyModule
{ path: '', component: LazyComponent },
{ path: 'child', component: LazyChildComponent }
  • Navigate to translated version of /lazy/child in language other than en e.g.
/de/muessiges/kind

Current behavior Router reports NavigationError as route has not been translated at the moment of route matching.

Expected behavior All child lazy routes should be translated and Router should properly find requested route.

Explanation LocalizeRouter currently listens to RouteConfigLoadEnd, but this event only signals that child module has been loaded. Child routes are still not available. Additionally RouteConfigLoadEnd returns only requested route, not the module factory from which the child routes could be extracted.

Further steps Discuss with Angular team on possible solutions.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:47 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
meeroslavcommented, Nov 11, 2017

Finally yes. I will implement the solution in the upcoming days.

5reactions
TimeTravelersHackedMecommented, Jan 22, 2019

Looks like this is not working with nested lazy loading… top level lazy loaded components get loaded but lazy loaded components with routes declared with forChild(routes) get the “Cannot match any routes” error

Here’s an example where the terms/privacy policy page work as expected but the lazy loaded module breaks with the Cannot match any routes error:

import { Location } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { LocalizeRouterModule, LocalizeRouterSettings, LocalizeParser } from 'localize-router';
import { LocalizeRouterHttpLoader } from 'localize-router-http-loader';

export function HttpLoaderFactory(translate: TranslateService, location: Location, settings: LocalizeRouterSettings, http: HttpClient) {
  return new LocalizeRouterHttpLoader(translate, location, settings, http);
}

const routes: Routes = [
  { path: '', loadChildren: './pages/home/home-tabs/home-tabs.module#HomeTabsPageModule' },
  { path: '', loadChildren: './pages/category/category-tabs/category-tabs.module#CategoryTabsPageModule' },
  { path: 'terms', loadChildren: './pages/terms/terms.module#TermsPageModule' },
  { path: 'privacy', loadChildren: './pages/privacy-policy/privacy-policy.module#PrivacyPolicyPageModule' },
  { path: 'search', loadChildren: './pages/search/search.module#SearchPageModule' },
  { path: 'video', loadChildren: './pages/video/video.module#VideoPageModule' }
];

@NgModule({
  imports: [
    LocalizeRouterModule.forRoot(routes, {
      parser: {
        provide: LocalizeParser,
        useFactory: HttpLoaderFactory,
        deps: [TranslateService, Location, LocalizeRouterSettings, HttpClient]
      }
    }),
    RouterModule.forRoot(routes)
  ],
  exports: [LocalizeRouterModule, RouterModule]
})
export class AppRoutingModule { }

And here’s the lazy loaded module’s routes:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import { LocalizeRouterModule } from 'localize-router';

import { HomeTabsPage } from './home-tabs.page';

const routes: Routes = [
  {
    path: 'browse',
    component: HomeTabsPage,
    children: [
      {
        path: 'popular',
        children: [
          {
            path: '',
            loadChildren: '../popular/home-popular.module#HomePopularPageModule'
          }
        ]
      },
      {
        path: 'latest',
        children: [
          {
            path: '',
            loadChildren: '../latest/home-latest.module#HomeLatestPageModule'
          }
        ]
      },
      {
        path: 'trending',
        children: [
          {
            path: '',
            loadChildren: '../trending/home-trending.module#HomeTrendingPageModule'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/browse/popular',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/browse/popular',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [
    LocalizeRouterModule.forChild(routes),
    RouterModule.forChild(routes),
    TranslateModule
  ],
  exports: [RouterModule]
})
export class TabsPageRoutingModule {}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular nested routing with lazy loading issue - Stack Overflow
I had a reference to my lazy loaded module in another part of my application. The lowest level lazy-load module had a route...
Read more >
How to Nest Lazy-Loaded Modules - Medium
Lazy -loading modules and routing in Angular is a pretty well-covered and well-rehearsed topic. Here I'll explain how to pull off nested ......
Read more >
Nested Lazy Loaded Routing - StackBlitz
Demonstrates nested routing with lazy-loading.
Read more >
36. Lazy Loading | Nested Routing | Angular (Tamil) - YouTube
Explaining: Lazy loading with Nested routing concept explained here in Angular with steps. Multiple router-outlet were also explained.
Read more >
Lazy-loading feature modules - Angular
You can opt into using string-based lazy loading ( loadChildren: './path/to/module#Module' ) by including the lazy-loaded routes in your tsconfig file, which ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found