我有一种情况,我们的主应用程序延迟加载其他模块:
//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}
])
现在,配置文件模块中包含一些类模块所必需的组件。
//Profile NgModule
RouterModule.forChild(
[
{path:'', component:ProfileComponent,
])
//Classroom NgModule
imports: [
ProfileModule,
RouterModule.forChild(
[
{path:'', component:ClassroomComponent} //this requires a component in ProfileModule
])
]
这编译得很好,但是当我尝试导航到“/classroom”时,我得到的只是 ProfileComponent
我想这是因为 ProfileModules 路由配置与 ClassroomModule 路由配置相结合。有什么办法可以防止这种情况发生吗?如果可能,我宁愿不必从 ProfileModule 中删除所有共享组件并将它们放入新的共享模块中。
最佳答案
我发现最好的方法是创建一个模块,它执行原始模块的所有声明但不导入路由。
<Module Name>WithoutRouting.ts
然后我创建另一个模块
<Module Name>.ts
我在哪里导入路由和 <Module Name>WithoutRouting.ts
模块。
然后当我想使用带有路由的模块时,例如当延迟加载时我使用 <Module Name>.ts
如果我想直接导入模块,我使用 <Module Name>WithoutRouting.ts
.
关于angular - 导入没有路由的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44284294/