尝试在angular2 rc中延迟加载模块。应用程序。该模块本身包含路由。该应用程序试图在单击其注册的路径时延迟加载模块,但会立即抛出
Error: Uncaught (in promise): Error: Cannot find 'default' in './app/test/test.module'
app中的路由定义如下:
export const routes: Routes = [
{ path: '', component: StartComponent },
{ path: 'route1', component: Route1Component },
{ path: 'test', loadChildren: './app/test/test.module'},
{ path: '**', component: StartComponent } //page not found
];
TestModule包含以下路线:
export const TestRoutes: Routes = [
{ path: '', redirectTo: 'overview' },
{ path: 'overview', component: TestOverviewComponent },
{ path: 'moredata', component: MoreDataComponent }
];
我删除了redirectTo并将默认路由指向真正的组件而没有运气。 还尝试用像
这样的孩子来定义路线export const AdminRoutes: Routes = [
{
path: 'test', component: TestComponent,
children: [
{ path: '', redirectTo: 'overview' },
{ path: 'overview', component: TestOverviewComponent },
{ path: 'moredata', component: MoreDataComponent }
]
}
];
同样的结果。
任何提示可能有什么问题?加载模块急切地按预期工作。
此致
答案 0 :(得分:31)
您必须将模块的导出类名添加到loadChildren字符串,例如
{ path: 'test', loadChildren: './app/test/test.module'},
更改为
{ path: 'test', loadChildren: './app/test/test.module#TestModule'},
如官方文档中所述https://angular.io/docs/ts/latest/guide/router.html
如果我们仔细观察loadChildren字符串,我们可以看到它映射 直接到我们之前构建的crisis-center.module文件 我们的危机中心特色区域。在我们使用的文件的路径之后 #表示文件路径的结束位置,并告诉路由器名称 我们的CrisisCenter NgModule。如果我们看看我们的危机中心。模块 文件,我们可以看到它与我们导出的NgModule类的名称相匹配。
某些博文中缺少此部分,例如
https://angularjs.blogspot.de/2016/08/angular-2-rc5-ngmodules-lazy-loading.html
答案 1 :(得分:6)
将模块文件./app/test/test.module.
修改为
export default class TestModule{}
你必须跳过"默认"关键字。
答案 2 :(得分:1)
或者你还能做什么,
export const AdminRoutes: Routes = [
{ path: '', redirectTo: 'test', pathMatch: 'full'}, //<----added this
{
path: 'test', component: TestComponent,
children: [
{ path: '', redirectTo: 'overview' },
{ path: 'overview', component: TestOverviewComponent },
{ path: 'moredata', component: MoreDataComponent }
]
}
];