我有一个情况,我们的主应用程序懒洋洋地加载其他模块:
//main NgModule
RouterModule.forRoot(
[
{path:'profile', loadChildren:'path/to/profile.module#ProfileModule},
{path:'classroom', loadChildren:'path/to/classroom.module#ClassroomModule},
{path:'tests', loadChildren:'path/to/test.module#TestsModule}
])
Run Code Online (Sandbox Code Playgroud)
现在,配置文件模块中有一些组件,它们是Classroom模块所必需的.
//Profile NgModule
RouterModule.forChild(
[
{path:'', component:ProfileComponent,
])
//Classroom NgModule
imports: [
ProfileModule,
RouterModule.forChild(
[
{path:'', component:ClassroomComponent} //this requires a component in ProfileModule
])
]
Run Code Online (Sandbox Code Playgroud)
这很好地编译,但是当我尝试导航到'/ classroom'时,我得到的是ProfileComponent
我想这是因为ProfileModules路由配置与ClassroomModule路由配置相结合.有没有办法阻止这种情况发生?我不希望从ProfileModule中删除所有共享组件,并在可能的情况下将它们放入新的共享模块中.
我发现这样做的最好方法是创建一个模块,它执行原始模块的所有声明,但不导入路由.
<Module Name>WithoutRouting.ts
Run Code Online (Sandbox Code Playgroud)
然后我创建了另一个模块
<Module Name>.ts
Run Code Online (Sandbox Code Playgroud)
我导入路由和<Module Name>WithoutRouting.ts模块的位置.
然后,当我想使用带路由的模块时,例如当我使用延迟加载时<Module Name>.ts,如果我想直接导入模块我使用<Module Name>WithoutRouting.ts.
尝试反转导入的位置
//Classroom NgModule
imports: [
RouterModule.forChild(
[
{path:'', component:ClassroomComponent}
],
ProfileModule
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1956 次 |
| 最近记录: |