Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(router): Run loadComponent and loadChildren functions in the… #62133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

atscott
Copy link
Contributor

@atscott atscott commented Jun 18, 2025

… route's injection context

This updates the loader code to run the loadComponent and loadChildren functions in the appropriate injection context for the route.

A primary motiviation for this feature is to bring loadChildren with
standalone components and the routes array to
feature-parity with what was possible when using loadChildren and a
module that provided routes via the ROUTES token and a factory
function (which would have injection context).

fixes #51532

@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: router labels Jun 18, 2025
@atscott atscott added feature Issue that requests a new feature router: lazy loading target: minor This PR is targeted for the next minor release and removed detected: feature PR contains a feature commit labels Jun 18, 2025
@ngbot ngbot bot added this to the Backlog milestone Jun 18, 2025
… route's injection context

This updates the loader code to run the `loadComponent` and
`loadChildren` functions in the appropriate injection context for the
route.

A primary motiviation for this feature is to bring `loadChildren` with
standalone components and the routes array to
feature-parity with what was possible when using `loadChildren` and a
module that provided routes via the `ROUTES` token and a factory
function (which would have injection context).

fixes angular#51532
@atscott atscott force-pushed the loadComponentloadChildrenInjectionContext branch from f1d52cc to 593a85b Compare June 18, 2025 19:56
@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Jun 18, 2025
@eneajaho
Copy link
Contributor

This is a lovely feature! Thank you!

I have 2 questions:

  1. Would it be possible to register a callback function in the router (as a provider) to be called everytime a loadComponent/loadChildren gets called? Having the loadChildren run in the injection context makes the current hacks needed to disappear, as we can wrap every loadComponent/loadChildren in a function that can transform the result (so this PR already is a win). (Talking about this issue fix(router): add customRouteProcessor wip  #53312)
  2. Since we will be able to access DI and transform what can the result be for the loadChildren, does it mean this issue also gets solved Hook into router config loading #31800, or you see them as two different issues?

@atscott
Copy link
Contributor Author

atscott commented Jun 18, 2025

Would it be possible to register a callback function in the router (as a provider) to be called everytime a loadComponent/loadChildren gets called? Having the loadChildren run in the injection context makes the current hacks needed to disappear, as we can wrap every loadComponent/loadChildren in a function that can transform the result (so this PR already is a win). (Talking about this issue fix(router): add customRouteProcessor wip #53312)

Possible, but a completely separate feature request :)

Since we will be able to access DI and transform what can the result be for the loadChildren, does it mean this issue also gets solved #31800, or you see them as two different issues?

I think that issue is looking more for a one-stop shop transform, such as what might be achieved with something like in your first point. You can get what it's asking for by redoing the necessary transform on every route inside the loadChildren, but it's only enforced by convention rather than being a hook that can guarantee the expected outcome. i.e. localized-router would need to document something like "every time you load children, pass your dynamic import to X helper function so it becomes: loadChildren: localize(() => import('./feature-routes'))" Maybe that’s good enough though… it’s relatively easy to enforce with a simple linter check

@atscott atscott marked this pull request as ready for review June 20, 2025 18:41
@atscott atscott requested a review from alan-agius4 June 20, 2025 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: router detected: feature PR contains a feature commit feature Issue that requests a new feature router: lazy loading target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Injection Context for loadChildren and loadComponent functions
2 participants