Angular 使用 Webpack - loadChildren 找不到模块
Angular with Webpack - loadChildren can't find the module
我正在尝试加载我购买的与 Angular4 一起使用的管理员,但是我在使用 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 {}
当我 运行 应用程序时,我在控制台中收到此消息:
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 : "/"
我知道模块的路径是正确的,因为我可以在同一文件中毫无问题地导入此模块:
import {IndexModule} from './pages/default/index/index.module';
我的 Webpack 配置使用它来处理打字稿:
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'
]
}
我做错了什么我没有注意到吗?
延迟加载模块的位置应从 app
文件夹开始。
尝试像这样将 app
关键字添加到您的字符串中
loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},
另外我觉得你这里少了一个大括号
{
"path": "",
"component": ThemeComponent,
"canActivate": [AuthGuard],
"children": [
{
"path": "index",
loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},\
}
]
} <-------- ,
如果您使用的是 webpack,则需要 angular-router-loader:
loaders: [
{
test: /\.ts$/,
loaders: [
'awesome-typescript-loader',
'angular-router-loader'
]
}
]
创建一个加载额外路由的模块:
const routeModule:ModuleWithProviders = RouterModule.forChild(routes);
@NgModule({
imports: [ routeModule ],
declarations: [ ... ]
})
export class InboxModule{ }
创建你的路由并引用延迟加载的模块(路径是相对的):
import { Routes } from '@angular/router';
export const routes: Routes = [
{ path: 'index', loadChildren: './theme/pages/default/index/index.module#IndexModule' }
];
我发现我可以使用函数导入,而不是使用模块路径导入。这样就成功了:
{
"path": "index",
"loadChildren": () => IndexModule
}
我正在尝试加载我购买的与 Angular4 一起使用的管理员,但是我在使用 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 {}
当我 运行 应用程序时,我在控制台中收到此消息:
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 : "/"
我知道模块的路径是正确的,因为我可以在同一文件中毫无问题地导入此模块:
import {IndexModule} from './pages/default/index/index.module';
我的 Webpack 配置使用它来处理打字稿:
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'
]
}
我做错了什么我没有注意到吗?
延迟加载模块的位置应从 app
文件夹开始。
尝试像这样将 app
关键字添加到您的字符串中
loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},
另外我觉得你这里少了一个大括号
{
"path": "",
"component": ThemeComponent,
"canActivate": [AuthGuard],
"children": [
{
"path": "index",
loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},\
}
]
} <-------- ,
如果您使用的是 webpack,则需要 angular-router-loader:
loaders: [
{
test: /\.ts$/,
loaders: [
'awesome-typescript-loader',
'angular-router-loader'
]
}
]
创建一个加载额外路由的模块:
const routeModule:ModuleWithProviders = RouterModule.forChild(routes);
@NgModule({
imports: [ routeModule ],
declarations: [ ... ]
})
export class InboxModule{ }
创建你的路由并引用延迟加载的模块(路径是相对的):
import { Routes } from '@angular/router';
export const routes: Routes = [
{ path: 'index', loadChildren: './theme/pages/default/index/index.module#IndexModule' }
];
我发现我可以使用函数导入,而不是使用模块路径导入。这样就成功了:
{
"path": "index",
"loadChildren": () => IndexModule
}