【问题标题】:add more children routes from a module to another将更多子路由从一个模块添加到另一个
【发布时间】:2018-06-11 14:39:00
【问题描述】:

假设我有一个名为 Parent 的模块:

const routes: Routes = [
    {
        path: '',
        component: ParentComponent,
        children: [
            {
                path: '/:id',
                component: ChildComponent
            }
        ]
    }
]

@NgModule({
  declarations: [
    ParentComponent,
    ChildComponent,
  ],
  imports: [
    RouterModule.forChild(routes),
  ],
})
export class ParentModule {
}

这个模块有一些子路由可以提供很好的路由体验,但它有一个问题:我不知道如何从另一个模块添加更多子路由。

假设我想添加一个模块ChildDetailsModule,它声明一个ChildDetailsComponent,并想将它绑定到/:id/details,然后我就卡住了。因为我不想让ParentModule 知道这个组件,因为它是由另一个模块提供的,所以我希望能够通过从我的AppModule 的导入模块中删除ChildDetailsModule 来拔掉它。

有可能实现吗?我在当前的 Angular 文档中没有找到方法,我肯定必须这样做,因为我需要一个内部路由器插座,同时我还需要能够使用其他模块为这个内部路由器插座提供额外的路由。

【问题讨论】:

    标签: angular angular6 angular-router


    【解决方案1】:

    您必须将角度路线视为一棵树,因此必须存在父级。但是,您正在寻找的是延迟加载。

    在父模块上使用延迟加载来加载路由中的完整模块。这样,当用户导航到该路由时,指定的模块将提供她自己的路由,您可以添加一些其他子路由或 wathever。

    例如:

    { 
      path: 'parent/newmodule',
      Loadchildren: 'Newmodulepath#NewModule'
    }
    

    【讨论】:

    • 这是在子模块还是在父模块?
    • @Supamiu 你应该设计你的路由树,然后调整角度。你的路由树级别应该匹配你的组件和模块,所以你可以把它放在更适合的地方
    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2015-02-20
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多