导入模块没有路由

VFe*_*ein 11 angular

我有一个情况,我们的主应用程序懒洋洋地加载其他模块:

//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中删除所有共享组件,并在可能的情况下将它们放入新的共享模块中.

rog*_*oom 8

我发现这样做的最好方法是创建一个模块,它执行原始模块的所有声明,但不导入路由.

<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.

  • 此解决方案的一些细节:不要忘记在“ModuleWithoutRouting”中“导入”“RouterModule”,否则您将无法使用“routerLink”或其他类似的东西 (2认同)

Mic*_*tto 1

尝试反转导入的位置

 //Classroom NgModule
    imports: [
      RouterModule.forChild(
      [
        {path:'', component:ClassroomComponent} 
      ],
      ProfileModule
)
Run Code Online (Sandbox Code Playgroud)

  • 试过了,似乎没有什么区别 (10认同)