Router - make loadChildren accept params provided by DI tokens
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:10
- Comments:7 (3 by maintainers)
ROUTES
itself is a token and can use arbitrary deps already, just needuseFactory
instead ofuseValue
: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
.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.