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.

Router - make loadChildren accept params provided by DI tokens

See original GitHub issue

I’m submitting a …

[ ] bug report => search github for a similar issue or PR before submitting
[x] 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 LoadChildrenCallback is a function returning a few different things including Promise, Observable

https://github.com/angular/angular/blob/2.4.x/modules/@angular/router/src/config.ts#L313-L314

The way to do lazy load modules is

{ path: 'route_name', loadChildren: () => System.import('./path/to/yourmodule').then(mod => mod.ModuleName) },
or
{ path: 'route_name', loadChildren: () => './path/to/module#ModuleName' },

Expected behavior LoadChildrenCallback can take in a list of params which are provided as a DI token, similar to canActivate

something like

{
  path: 'route_name',
  loadChildren: (route, http, foo, bar) => {
    return http.get('some/api/' + route.queryParam.id)
      .then(data => {
        if (data.pageType === 'PAGE_TYPE_1') {
          return System.import('./path/to/PremiumMemberPage').then(mod => mod.ModuleName)
        } else {
          return System.import('./path/to/GuestMemberPage').then(mod => mod.ModuleName)
        }
      })
  },
  loadChildrenTokens: [ActivatedRoute, Http, Foo, Bar]
}

Minimal reproduction of the problem with instructions N/A

What is the motivation / use case for changing the behavior? I posted this question on SO. I am not sure such a similar request has been made before my search did not yield me anything good. Feel free to close the issue if this has already been discussed and kindly share how I can get result requested here via alternate means

Please tell us about your environment: N/A

  • Angular version: 2.4 + (or even in 4+)

  • Browser: all

  • Language: TypeScript

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
trotylcommented, Aug 6, 2018

ROUTES itself is a token and can use arbitrary deps already, just need useFactory instead of useValue:

https://stackblitz.com/edit/angular-ia14rd?file=src/app/app-routing.module.ts

There’s no need to inject for each loadChildren individually as they’re all in the same module-level injector.

Having asynchronous route configuration is an orthogonal request, can be achieved with APP_INITIALIZER.

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

Route - Angular
A configuration object that defines a single route. A set of routes are collected in a Routes array to define a Router configuration....
Read more >
Angular 4 Lazy Loading with parameters - Stack Overflow
Great! It worked for my project this example for RouterModule.forChild was very helpful. A parameter in the lazy loading path for the module...
Read more >
Mastering Angular dependency injection ... - Ultimate Courses
Providers in Angular are key to how we develop our applications, and injecting dependencies can be done in various ways.
Read more >
Parameterised Routes • Angular - codecraft.tv
Parameterised Route Configuration; Activated Route; Optional Parameters ... Copy import {ActivatedRoute} from "@angular/router"; . . . constructor(private ...
Read more >
Module core - UI-Router
Returns Provider[] ... When a DI token is defined as multi: true, the child injector can add new values for the token. This...
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