Repository navigation
Description
Bug Report or Feature Request (mark with an x)
- [X] bug report -> please search issues before submitting
- [ ] feature request
Versions.
@angular/cli: 1.0.0-rc.2
node: 6.9.1
os: win32 x64
@angular/common: 4.0.0-rc.5
@angular/compiler: 4.0.0-rc.5
@angular/core: 4.0.0-rc.5
@angular/forms: 4.0.0-rc.5
@angular/http: 4.0.0-rc.5
@angular/platform-browser: 4.0.0-rc.5
@angular/platform-browser-dynamic: 4.0.0-rc.5
@angular/router: 4.0.0-rc.5
@angular/compiler-cli: 4.0.0-rc.5
Repro steps.
I'm reposting this issue to @angular repository, because I've found out that the issue is more related to
compiler-clithanangular-cliitself.
First of all, I'd like to let you know that there were already two discussions related to this topic (#4686 and #4541), however #4686 was closed by @filipesilva due to the similarities with #4541.
Actually, #4541 points to slightly different topic (it's a feature request), which could potentially cause ignoring the issue that I'm trying to report.
In order to reproduce this error, first install the @nglibs/i18n-router package (which is aimed to be a route internationalization solution for Angular v2.x.x and v4.x.x), and then set up the module configuration in app.module.ts.
I'll not copy the whole code here, as solution files are located on angular-cli branch of @nglibs/example-app. The solution depends on @angular v2.4.7 & @angular/cli v1.0.0-beta.30 but the issue occurs on @angular v4.0.0-rc.5 &@angular/cli v1.0.0-rc.2 as well.
Then, in a feature module (here used AboutModule as an example), use the @nglibs/i18n-router by importing I18NRouterModule and replace the RouterModule.forChild(routes) import with I18NRouterModule.forChild(routes, 'about') - which provides ROUTES and ANALYZE_FOR_ENTRY_COMPONENTS to Angular the same way that the router module does (see below):
about.module.ts
import { I18NRouterModule } from '@nglibs/i18n-router';
@NgModule({
imports: [
CommonModule,
// RouterModule.forChild(routes)
I18NRouterModule.forChild(routes, 'about')
],
declarations: [
AboutComponent,
AboutUsComponent,
AboutBananaComponent,
AboutApplePearComponent
]
})
The log given by the failure.
Error: Cannot read property 'loadChildren' of undefined
Mention any other details that might be useful.
First of all, this issue does not occur when compiling the app on JiT. Also, using ngc from the command line, and ng-router-loader in webpack configuration results with success on AoT.
The issue occurs only with @ngtools/webpack.
The solution files to repro this issue are located on angular-cli branch of @nglibs/example-app.
After some research, and in-depth digging of @ngtools/webpack source code, I've found the lines of code which is responsible for the issue.
Passing a factory method (useFactory) for the DI token ROUTES causes the discovery of lazy-routes to fail:
i18n-router.ts
static forChild(routes: Routes, moduleKey: string): ModuleWithProviders {
return {
ngModule: I18NRouterModule,
providers: [
provideRoutes([]),
...
{
provide: ROUTES,
useFactory: (provideChildRoutes),
deps: [I18NRouterService, RAW_ROUTES, MODULE_KEY],
multi: true
},
{
provide: ANALYZE_FOR_ENTRY_COMPONENTS,
useValue: routes,
multi: true
}
]
}
}
However, if I provide child routes unprocessed (simply pass useValue: routes instead of useFactory: ..., deps: ...), then the @ngtools/webpack can find the lazy routes.
Analyzing the issue line-by-line, I came across with the @ngtools/webpack, the listLazyRoutesOfModule function.
Desired functionality.
The listLazyRoutesOfModule function of @ngtools/webpack should be able to discover lazy routes supplied by a factory method (useFactory) for the DI token ROUTES.
Activity
sod commentedon Mar 20, 2017
I also once tried to get that into angular #10983.
I heard the angular team has internationalization in focus for angular 5.0.0. That might bring solutions.
fulls1z3 commentedon Mar 20, 2017
Hi @sod, I've read the details of your PR and with some luck debugging the
ngtools_impl.ts, I've found out the solution.While the angular team has their own i18n focus, I'll publish a PR and hope it will be merged during the next release.
[-]compiler-cli: @ngtools/webpack should accept `useFactory` for the DI token `ROUTES`[/-][+]compiler-cli: ngtools should accept `useFactory` (lazy routes)[/+]ocombe commentedon Mar 27, 2017
Actually I think that adding the locale to the routes should probably be something that angular offers for i18n. What do you think @vicb ?
1 remaining item
alxhub commentedon May 28, 2020
useFactoryfunctions are not statically analyzable, so the original solution as proposed does not work. But there's a larger question here of how router and i18n collaborate on locales in the route itself.petebacondarwin commentedon Oct 23, 2020
@fulls1z3 - I am sorry that this issue has been left so long. I feel that it is possibly outdated and no longer relevant. Since this issue was created, the way that lazy routes are loaded has changed, ivy has been released, i18n has been implemented and updated. Can you clarify if this is still an issue for you and maybe update it to be relevant to the current version of Angular?
petebacondarwin commentedon Nov 23, 2020
Closing due to lack of response.
angular-automatic-lock-bot commentedon Dec 24, 2020
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.