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.

routing + lazy loading - angular is ignoring modules in the middle of path

See original GitHub issue

I’m submitting a … (check one with “x”)

[x ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior I got problem with routing inside lazy loaded modules.

Here is my “root” routes:

const routes: Routes = [
  {path: '', redirectTo: 'admin', pathMatch: 'full' },
    {path: 'admin', loadChildren: 'app/admin.module#AdminModule'}
];

@NgModule({
  imports: [
      RouterModule.forRoot(routes)
  ],
  exports: [
      RouterModule
  ]
})
export class MainRoutingModule {}

So what I expect from my app, is to redirect empty path to /admin - this works fine, module is loaded.

in AdminModule I got another redirect:

const routes: Routes = [
    {path: '', component: AdminComponent, children: [
        {path: '', redirectTo: 'app', pathMatch: 'full' },
        {path: 'app', loadChildren: './app/app.module#AppModule', canActivate: [ AuthGuard ] },
        {path: 'login', component: LoginComponent}
    ]},
];

@NgModule({
    imports: [
        RouterModule.forChild(routes),
        AppModule
    ],
    exports: [
        RouterModule
    ]
})
export class AdminRoutingModule {}

Here I got two scenarios:

  • if user is authenticated redirect to app - so the whole path would be /admin/app
  • otherwise redirect to login - so the whole path should be /admin/login

and finally routing in admin application:

const routes: Routes = [
  { path: '', component: AppComponent, children: [
    { path: '',  redirectTo: 'home', pathMatch: 'full' },
    { path: 'home', component: HomeComponent},
  ]}
];

And now problems starts. If I will enter my side with url localhost:3000/admin/app/(*) everything works as expected, AuthGuard is invoked, url is ok, in case that user is unauthenticated he gets redirect to login page.

But!: If I will enter localhost:3000 app redirects me to localhost:3000/admin/home (app is missing) and AuthGuard is not invoked. It looks like Angular is ommiting the admin-routing.module and goes directly to the last one.

Expected behavior angular should respect all modules described between root and last one.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
reppnerscommented, Jul 3, 2017

@maciejtreder

I struggled with a problem quite similar to yours until I found out that I statically imported the module that should be lazy loaded in a parent module. Double check that you’re not importing the lazy loaded module statically. Sounds like a good candidate for a codelyzer rule.

3reactions
mariuslazar93commented, Feb 11, 2018

I had some sort of the same issue, but understanding what should be and what shouldn’t be imported solved my issue.

Make sure you don’t import AdminModule inside the your main/root app component (where you import MainRoutingModule). You also don’t need to import AppModule inside AdminRoutingModule and also make sure you don’t import AppModule inside the AdminModule (where you import AdminRoutingModule). Since they are lazy loaded modules (both AdminModule and AppModule), they shouldn’t be imported anywhere.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular: Issues with Lazy loading different route modules for ...
I have an app that has a route called /opportunity/:id . Depending on a value fetched from the API, I need to load...
Read more >
Lazy-loading feature modules - Angular
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. AppRoutingModule (excerpt)
Read more >
In-app navigation: routing to views - Angular
The first property, path , defines the URL path for the route. ... You can configure your routes to lazy load modules, which...
Read more >
You'll soon ( @angular v15) be able to lazy-load a route by ...
Is this only for loadComponent ? Not loadChildren which loads modules? If so, it's a very specific use case where you lazy load...
Read more >
Angular - Lazy loading your feature modules - LinkedIn
With Angular, the router is what allows us to lazy load. We call it “lazy” because it's not “eagerly” loading - which would...
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