-
Notifications
You must be signed in to change notification settings - Fork 12k
Cannot read property 'loadChildren' of undefined #4686
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
Comments
That's where the lazy route discovery is happening: I wasn't even aware there're other alternatives to this, but to start with, it seems to be considering the
No idea about I wouldn't expect focus on this one from the core team before v1.0 final (could be wrong). Would you have any idea extending the above file to cater for your case? |
I think this is essentially the same issue as #4541, please have a look. |
Hi @filipesilva, I've read #4541 and @christopherthielen talks about more or less a similar functionality. Currently waiting for updates, and many thanks for your attention 👍 |
First of all I'd like to say thanks to @christopherthielen, for all the details he provided on #4541. This information was very helpful while analyzing the issue with As far as I can progress, I've found out that if I provide child routes unprocessed, then the
Now, the challenge is to get the |
I had the error. Found the cause in my case, want to share
The error is the
at the end of the 4th line, which delivers an empty element in the Routes array, which delivers the loadChildren error. |
I had the same error as well, but the problem was I had move the app-router.module.ts outside app folder. |
I had the same error but yet another cause. I was dynamically assigning a key to the const routes: Routes = [{
path: '',
component: SomeComponent,
children: [{
path: 'blah',
component: BlahCreateComponent,
resolve: {
[SOME_CONSTANT]: SomeResolver, // <-- you can't do this
},
}],
}] I was only able to track down the issue by doing |
@tomsaleeba and how you fixed it? you just deleted the resolve part? |
@tomsaleeba
it's properly to set a key as direct definition or instant string value like |
@thiagofrancisquete as @KorenB mentioned, it seems like you can't dynamically set the key of the object (using that syntax with the square braces As a concrete example, assume I was trying to assing the dynamic key like this const SOME_CONSTANT = 'theKey'
...
resolve: {
[SOME_CONSTANT]: SomeResolver,
},
... ...to fix it, you need to do this: ...
resolve: {
theKey: SomeResolver,
},
... |
I had faced same issue. In My case I had not included my Route resolver in provider property of NgModule. |
as I commented @bevlison I also had syntax error |
One final gotcha to contribute: import { urls } from 'consts'
const { topLevelRoutes } = urls
const routes = [
{ path: topLevelRoutes.login, loadChildren: 'path/login.module#LoginModule' },
{ path: topLevelRoutes.dashboard, loadChildren: 'path/dashboard.module#DashboardModule' },
{ path: '**', redirectTo: topLevelRoutes.dashboard }
] Fails with the aforementioned error. import { urls } from 'consts'
// const { topLevelRoutes } = urls
const routes = [
{ path: urls.topLevelRoutes.login, loadChildren: 'path/login.module#LoginModule' },
{ path: urls.topLevelRoutes.dashboard, loadChildren: 'path/dashboard.module#DashboardModule' },
{ path: '**', redirectTo: urls.topLevelRoutes.dashboard }
] Works fine. Not sure if it's only a dev server thing and works with a proper build or not. Also not sure about any permutations. Obviously not a pain point, just wanted to add this in case somehow runs into a similar circumstance. |
I came to this issue with a similar problem and actually found my fix here. The problem I was having was I did not have the |
Sure, in my case this did the trick:
|
run angular using |
I faced this error as well but for another reason. I had to convert it to a string: |
I know this thread is becoming like stack overflow, but it's the first hit on Google. Here was my scenario. Cause: routes was not defined as a constant. I had a route configuration like: let routes: Routes;
routes = [
{
path: '',
children: [
{
path: '',
children: [
{
path: '',
children: [
{path: '', component: HeaderComponent, outlet: 'header'},
{path: '', component: FooterComponent, outlet: 'footer'}
// Routes that display a header and footer go here.
]
},
{
path: '',
children: [
{
path: '/frame',
children: [{
path: 'search',
loadChildren: () => import('./search/search.module').then(module => module.SearchModule)
}]
}
]
}
]
}
]
}
]; Very specifically, while I was in let routes: Routes;
routes = [
{
path: '',
children: [
{
path: '',
children: [
{
path: '',
children: [
{path: '', component: HeaderComponent, outlet: 'header'},
{path: '', component: FooterComponent, outlet: 'footer'}
// Routes that display a header and footer go here.
]
},
{
path: '',
children: [
{
path: '/frame',
children: [{
path: 'search',
loadChildren: () => import('./search/search.module').then(module => module.SearchModule)
}]
}
]
}
]
}
]
},
{
path: '**',
redirectTo: '/frame/search',
pathMatch: 'full'
}
]; The browser refreshed and there was a console error:
So sure enough, I had not added those imports to the app.module. I don't know if the error message presented simply by making any change or by adding this specific route change. I do know that if I ran After adding those imports I got another console error:
I fixed that: let routes: Routes
routes = [
{
path: '',
children: [
{
path: '',
children: [
{
path: '',
children: [
{path: '', component: HeaderComponent, outlet: 'header'},
{path: '', component: FooterComponent, outlet: 'footer'}
// Routes that display a header and footer go here.
]
},
{
path: '',
data: {header: false, footer: false},
children: [
{
path: 'frame',
children: [{
path: 'search',
loadChildren: () => import('./search/search.module').then(module => module.SearchModule)
}]
}
]
}
]
}
]
},
{
path: '**',
redirectTo: '/frame/search',
pathMatch: 'full'
}
]; And I was good to go (or so I thought).
Comparing the route files between a new project and mine, I noticed that routes was defined as a constant. I probably had copied in the routes from a prior project, or WebStorm modified the code somehow, not sure. So finally my working app-routes. const routes: Routes = [
{
path: '',
children: [
{
path: '',
children: [
{
path: '',
children: [
{path: '', component: HeaderComponent, outlet: 'header'},
{path: '', component: FooterComponent, outlet: 'footer'}
// Routes that display a header and footer go here.
]
},
{
path: '',
data: {header: false, footer: false},
children: [
{
path: 'frame',
children: [{
path: 'search',
loadChildren: () => import('./search/search.module').then(module => module.SearchModule)
}]
}
]
}
]
}
]
},
{
path: '**',
redirectTo: '/frame/search',
pathMatch: 'full'
}
]; |
By the way, using constants for defining the route props works if you compile with Ivy renderer. |
This should be working however, it is not so unusual to use constant keys for routing data that eventually you have to retrieve in other places of the application... My workaround was something like this:
This actually works because the constant key values are resolved before the routes definition so there are no compilation errors. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
OS?
Windows 10
Versions.
@angular/cli: 1.0.0-beta.30
node: 6.9.1
os: win32 x64
@angular/common: 2.4.7
@angular/compiler: 2.4.7
@angular/core: 2.4.7
@angular/forms: 2.4.7
@angular/http: 2.4.7
@angular/platform-browser: 2.4.7
@angular/platform-browser-dynamic: 2.4.7
@angular/router: 3.4.7
@angular/cli: 1.0.0-beta.30
@angular/compiler-cli: 2.4.7
Repro steps.
feature.module.ts
Using @nglibs/i18n-router to provide route translations for feature modules (lazy loaded).
The whole solution is located on github: angular-cli branch of @nglibs/example-app.
The log given by the failure.
Can't talk about a stack trace, but hope it will be helpful:
Mention any other details that might be useful.
@nglibs/i18n-router
does not work with@angular-cli
(yet), and giving the following error during [AoT compilation]:I suppose
@angular-cli
uses@ngtools/webpack
for [AoT compilation], expectingRouterModule.forChild(...)
to resolve lazy-loaded modules (withloadChildren
), which is replaced byI18NRouterModule.forChild(...)
- providing routes for feature modules instead.Here's the piece of code from i18n-router module line 72, which provides child routes.
It provides
ROUTES
andANALYZE_FOR_ENTRY_COMPONENTS
to Angular the same way that the router module does (see below):However, angular-cli seems insisting ignoring the provided child routes and firing errors.
To resolve this issue temporarily, I switched using [ng-router-loader]. Hence
@angular-cli
doesn't allow modifying the webpack configuration, I need to manually configure build tools (dev/prod sever, task runners, webpack, etc).I think
loadChildren
should be able to recognize routes provided withROUTES
andANALYZE_FOR_ENTRY_COMPONENTS
.The text was updated successfully, but these errors were encountered: