angular - 导入没有路由的模块

标签 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}
])

现在,配置文件模块中包含一些类模块所必需的组件。

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

相关文章:

Angular 4在重新路由到它时调用组件方法

angular - TensorflowJS : Error: The shape of dict ['images' ] provided in model. execute(dict) 必须是 [-1,224,224,3],但是是 [400,600,1]

javascript - highchart donut - 通过从 userService 搜索,单击切片打开页面

angular - 在 ionic 3 中创建自定义对话框/弹出窗口/警报

Angular 7、svg 和 sass 如何设置相对路径

Angular io(4) *ngFor first and last

angular - 如何为表 tbody 使用 cdk-virtual-scroll-viewport

angular - 更新到 Angular 9 失败

Angular 2 iso 日期格式

Angular react 形式数组,更改检测覆盖形式