Repository navigation
Closed
Labels
area: routereffort1: hoursfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationrouter: lazy loading
Milestone
Description
Which @angular/* package(s) are relevant/related to the feature request?
router
Description
What I have
- host application
- micro front-end applications that are loaded using
loadChildrenandloadConponent - localization using
@angular/localize
Problem
Need to load micro-frontend module or component with host application locale.
The LOCALE_ID token is used to determine the current locale. The first thing that comes to mind is to use the inject function to get the token value from the injector.
{
path: '...',
loadChildren: () => loadRemoteModule(`module-${inject(LOCALE_ID)}`, 'Module')
}But in the loadChildren and loadComponent functions, using the inject function causes an error.
NG0203: `inject()` must be called from an injection context, such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`.
Proposed solution
I propose to create an injection context for loadChildren and loadComponent functions when calling them
Alternatives considered
I am setting the locale to the window object as a fast solution.
{
provide: APP_INITIALIZER,
multi: true,
useFactory: () => {
const locale = inject(LOCALE_ID);
const windowRef = inject(WINDOW);
windowRef.__LOCALE_ID__ = AvailableLocales.includes(
locale as AvailableLocale
)
? locale
: AvailableLocale.EN;
return () => undefined;
},
}And then load micro-frontend:
{
path: '...',
loadChildren: () => loadRemoteModule(
`module_${window.__LOCALE_ID__}`,
moduleName
)
}But for SSR, I will have to get another solution.
Reacted by Vlad, Igor Katsuba, Marsel Khisamov, AMuravev, petrik1991, Vyacheslav Prisenko, Alex Inkin, hyperlife1119, Diego Bonura, Gerome Grignon and 65 more
Metadata
Metadata
Assignees
Labels
area: routereffort1: hoursfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationrouter: lazy loading
Activity
angular-robot commentedon Aug 28, 2023
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
Erbenos commentedon Oct 11, 2023
Alternatively one could setup routes by providing the ROUTES DI token manually via a factory function. In that factory function you can use inject and hence you can build the remote module's path dynamically.
chriswoodie commentedon Jan 4, 2024
This is crucial to let developers translate the paths of lazy loaded routes in applications that depend on SSR. Since the @angular/localize package doesn't provide any out-of-the-box solution for this, one must get hold of the
langviadocument.documentElement.lang(since it's set automatically when using different locales) and iterate over the routes and their children to change the paths. However, for that you need to injectDOCUMENT, which you can't, and there's no way of getting a workable injector in this context.Example of this:
Please implement this 🙏
Ketec commentedon Feb 20, 2024
It is also important for dynamic routes, especially with standalone components where you no longer have modules for each component.
I even tried with ENVIRONMENT_INITIALIZER + router.resetConfig (but it gives root routes where the current lazy child is not accessible to modify/add children to).
Can it pass injector to all the child routes when you configure it at the root?
Is there a chance it makes it into 17.x.x or more likely 18+?
Or is there even any idea/plan as to how this could be achieved?
Erbenos commentedon Feb 20, 2024
For such dynamic routes, you may instead want to provide ROUTES token manually through factory function. Refer to provideRouter at
angular/packages/router/src/provide_router.ts
Line 90 in d9a1a7d
Ketec commentedon Feb 20, 2024
The dynamic routes are needed for the lazy loaded child route (where previously that component module could use ROUTES token to map new children) but as mentioned in the closed bug ticket - this no longer works with standalone.
ROUTES factory is never called even if it's in the component routes.ts as providers.
looking at angular code:
angular/packages/router/src/router_config_loader.ts
Line 125 in d9a1a7d
It looks to me as if
injector, routeare passed to the loadChildren?Making sure these are available in the callback too would likely solve these issues.
Erbenos commentedon Feb 20, 2024
I see. Perhaps you would be able to get hold of nearest injector through ENVIRONMENT_INITIALIZER like this? https://stackblitz.com/edit/stackblitz-starters-tayqtx?file=src%2Fmain.ts I mean its ugly, but it gets the job done. This isn't really a first case when it would be handy to get the routerConfig callbacks ran within an injection context, another I can think of is #53944
Ketec commentedon Feb 21, 2024
ENVIRONMENT_INITIALIZER is not available in component providers. It is there in the route.ts (for the component), but you can't store local variables there.
It wouldn't be at the initial bootstrap either, but somewhere deeper (some child module routes).
Which is the "new" pattern to route to standalone components without modules.
ROUTES does not work here either anymore. ENVIRONMENT_INITIALIZER does - but can't store local variables.
Can't modify the child routes (resetRouter) in ENVIRONMENT_INITIALIZER since the loadChildren import content for this route is not a static list of children, and the subroutes are not yet available.
Erbenos commentedon Feb 21, 2024
See #54518 (comment), you don't need to put route subtrees into component providers. Instead, declare them outside the component providers and load just route definitions. I know you have to save the reference to injector manually, thats why I said its ugly. I am only offering a workaround here. As for the modifying the child route configs, I am not familiar with that, however the stackblitz concept I linked should allow you to setup route subtrees in dynamic manner when the tree is being resolved in at least some capacity.
Ketec commentedon Feb 21, 2024
https://stackblitz.com/edit/stackblitz-starters-fjgpj5?file=src%2Flazy-page%2Froutes.ts
An example. The lazy-page/routes.ts for reference would need dynamic children.
This cannot be done at the root bootstrap level. Bootstrap should not contain any code specific to that sub-component/micro module.
Erbenos commentedon Feb 21, 2024
The problem in your linked stackblitz is exactly something that the concept in stackblitz I linked allows you to work around.
Char2sGu commentedon Jun 29, 2024
This has the potential to enable async providers for child routes by invoking a request with HttpClient and configure the child route's
providerproperty according to the request.Char2sGu commentedon Jul 26, 2024
In case anyone is looking for an out-of-box workaround:
JSProto commentedon Sep 20, 2024
How do you like this solution?
Way of getting a workable injector in this context.
routes.ts
feat(router): Run `loadComponent` and `loadChildren` functions in the…
feat(router): Run `loadComponent` and `loadChildren` functions in the…
feat(router): Run `loadComponent` and `loadChildren` functions in the…
feat(router): Run `loadComponent` and `loadChildren` functions in the…
angular-automatic-lock-bot commentedon Jul 25, 2025
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.