Reputation: 2141
I'm trying load an admin I bought that works with Angular4, however I'm having some trouble configuring the project to run with Webpack:
const routes: Routes = [
{
"path": "",
"component": ThemeComponent,
"canActivate": [AuthGuard],
"children": [
{
"path": "index",
"loadChildren": './pages/default/index/index.module#IndexModule'
}
],
{
"path": "**",
"redirectTo": "404",
"pathMatch": "full"
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ThemeRoutingModule {}
when I run the app, I get this message in console:
NavigationError {id: 1, url: "/", error: Error: Cannot find module './pages/default/index/index.module'. at eval (eval at ./src/main/weba…} error : Error: Cannot find module './pages/default/index/index.module'. at eval (eval at ./src/main/webapp/admin/app lazy recursive (http://localhost:8080/admin/app/main.bundle.js:6:1), :5:9) at ZoneDelegate.invoke (webpack-internal:///./node_modules/zone.js/dist/zone.js:391:26) at Object.onInvoke (webpack-internal:///./node_modules/@angular/core/@angular/core.es5.js:4094:33) at ZoneDelegate.invoke (webpack-internal:///./node_modules/zone.js/dist/zone.js:390:32) at Zone.run (webpack-internal:///./node_modules/zone.js/dist/zone.js:141:43) at eval (webpack-internal:///./node_modules/zone.js/dist/zone.js:831:57) at ZoneDelegate.invokeTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:424:31) at Object.onInvokeTask (webpack-internal:///./node_modules/@angular/core/@angular/core.es5.js:4085:33) at ZoneDelegate.invokeTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:423:36) at Zone.runTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:191:47) id : 1 url : "/"
I know the path to the module is correct, as I can import this module without problem in the same file:
import {IndexModule} from './pages/default/index/index.module';
My Webpack config is using this to proccess the typescript:
module: {
rules: [{
test: /\.ts$/,
enforce: 'pre',
loaders: 'tslint-loader',
exclude: ['node_modules', new RegExp('reflect-metadata\\' + path.sep + 'Reflect\\.ts')]
},
{
test: /\.ts$/,
loaders: [
'awesome-typescript-loader',
'angular-router-loader?debug=true',
'angular2-template-loader'
]
}
Is there something wrong I'm doing I didn't notice?
Upvotes: 2
Views: 2009
Reputation: 2141
Instead of import using module path, I discovered I can import using function. This way it worked:
{
"path": "index",
"loadChildren": () => IndexModule
}
Upvotes: 0
Reputation: 52837
If you're using webpack, you need the angular-router-loader:
loaders: [
{
test: /\.ts$/,
loaders: [
'awesome-typescript-loader',
'angular-router-loader'
]
}
]
Create a module which loads additional routes:
const routeModule:ModuleWithProviders = RouterModule.forChild(routes);
@NgModule({
imports: [ routeModule ],
declarations: [ ... ]
})
export class InboxModule{ }
Create your routes and reference the lazy-loaded module (the path is relative):
import { Routes } from '@angular/router';
export const routes: Routes = [
{ path: 'index', loadChildren: './theme/pages/default/index/index.module#IndexModule' }
];
Upvotes: -1
Reputation: 3418
The location of your lazy-loaded modules should start at the app
folder.
Try to add the app
keyword to your string like this
loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},
Also I think you are missing a curly brace here
{
"path": "",
"component": ThemeComponent,
"canActivate": [AuthGuard],
"children": [
{
"path": "index",
loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},\
}
]
} <-------- ,
Upvotes: 0