如果你想偷懒加载模块,不导入它,你在这里所做的:如果你加载模块
import { LazyModule } from './lazy/lazy.module';
...
@NgModule({
imports: [ BrowserModule, RouterModule.forRoot(routes), LazyModule ]
(使用:
的src/app.tsimport
)它将被捆绑到主模块中而不是分离的块。
被替换为:
@NgModule({
imports: [ BrowserModule, RouterModule.forRoot(routes) ]
那么你的路线应该是这样的:
const routes = [
{
path: '',
component: MainComponent,
children: [
{
path: '',
component: DummyComponent
},
// should be rendered as a CHILD of my MainComponent .. !!
{
path: 'lazy',
loadChildren: 'src/lazy/lazy.module#LazyModule'
}
]
}
];
注意loadChildren
路径从src
开始了。
对于的src /懒/ lazy.module.ts: 你应该把你的路线确定(因为它是儿童需要的模块)但你的路线应该是空的,因为他们会通过以下后缀他们的父母的路线。 (这里:'/ lazy')。
所以,如果你把:
const routes = [
{
path: 'lazy',
component: LazyComponent
}
];
您希望匹配/lazy/lazy
使用你的懒惰组件,这是不是你想要的。
而是使用:
const routes = [
{
path: '',
component: LazyComponent
}
]
这里的工作Plunkr:https://plnkr.co/edit/KdqKLokuAXcp5qecLslH?p=preview
太好了!非常感谢! :) – mxii