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 load modules based on variables in path

See original GitHub issue

πŸš€ feature request

Relevant Package

I hope this feature request is for the @angular/router

Description

I don’t know if this is already there in Angular, but I couldn’t find any in the docs (or internet).

I think it’s nice to have an option to specify routes like this:

app-routing.module.ts

import { Routes } from '@angular/router';

const routes: Routes = [
  {
    path: 'theme-builder/:themeId',
    loadChildren:
      'src/app/theme-builders/{{themeId}}-builder.module#BuilderModule'
  }
]

So that the angular can load the corresponding module. For large applications, sometimes we may not be able to specify every possible loadChildren paths. Sometimes these variables in the path are coming from a database - and it may also have a corresponding loadChildren. But specifying all these combinations hard-coded in the app doesn’t feel like a scalable solution.

For clarity, the above code sample can be a replacement for:

const routes: Routes = [
  {
    path: 'theme-builder/theme-1',
    loadChildren:
      'src/app/theme-builders/theme-1-builder.module#BuilderModule'
  },
  {
    path: 'theme-builder/theme-2',
    loadChildren:
      'src/app/theme-builders/theme-2-builder.module#BuilderModule'
  },
  ...
]

Describe the solution you’d like

I’m aware of the AOT. I think I can specify these individual lazy loaded modules in the angular.josn to compile them ahead of time.

{
  "architect":{
    "build":{
      "options":{
        "lazyModules": [
          "src/app/theme-builders/theme-1-builder.module",
          "src/app/theme-builders/theme-2-builder.module"
          ...
          // or even better a glob pattern
        ]
      }
    }
  }
}

Describe alternatives you’ve considered

I came across this nice article. But it seems to be more complex and doesn’t support in-module-routing (I think).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dawidgaruscommented, Apr 7, 2019

@vajahath What’s the difference between explicitly listing all lazy modules in angular.json and router configuration? Also, what’s the point of having the paths provided from database if you still need to have those modules (the actual files) defined in your code, so they are kind of hardcoded anyways.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 4, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy-loading feature modules - Angular
This section introduces the basic procedure for configuring a lazy-loaded route. For a step-by-step example, see the step-by-step setup section on this page....
Read more >
How to lazy load module based on some condition using ...
I wrote this code in read-handler module So. when this module load, it's useFactory will execute and update ROUTES variable which is alreadyΒ ......
Read more >
Angular Lazy Loading β€” Not Only For The Lazy
Angular doesn't support lazy variables natively but it supports the lazy loading modules, which are way more important. Why?
Read more >
Manually Lazy Load Modules And Components In Angular
Based on this configuration data, multiple modules and/or components need to be lazy-loaded and its routes dynamically added to the application.
Read more >
Configuring Lazy Loaded Angular Modules - Thinktecture AG
Configuring Lazy Loaded Modules Β· the static method returns this (the module class) Β· we store the config in a static class variable....
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