gpt4 book ai didi

javascript - 如何 "mount"Angular 2 模块在路由层次结构中某个点的路由

转载 作者:搜寻专家 更新时间:2023-10-30 21:30:13 25 4
gpt4 key购买 nike

假设我有这样定义的主路由模块:

// app-routing.module.ts
const appRoutes: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: 'auth',
component: MasterPageComponent,
canActivate: CanActivateGuard
}
];

然后在另一个文件中有一个feature路由模块:

// feature-routing.module.ts
const featureRoutes: Routes = [
{
path: '/feature',
component: FeatureComponent
}
];

我怎样才能将我的功能模块中的路由“挂载”到我的主模块下,这样我就可以像这样访问它:

/auth/feature

从概念上讲,我正在寻找类似的东西:

RouterModule.forChildAtMountPoint(featureRoutes, '/auth');

我知道这可以通过延迟加载实现,但我希望这个模块能够被提前加载。

最佳答案

这非常简单,但据我所知仍然还没有documented .

尽管路由定义的 loadChildren 属性通常与 lazy loading 相关联, 它也可以被分配一个函数来返回一个急切加载的模块:

// app-routing.module.ts
const appRoutes: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: 'auth',
component: MasterPageComponent,
canActivate: [CanActivateGuard],
children: [
{
path: 'feature',
loadChildren: () => FeatureModule
}
]
}
];

// feature-routing.module.ts
const featureRoutes: Routes = [
{
path: ''
component: FeatureComponent
}
];

关于javascript - 如何 "mount"Angular 2 模块在路由层次结构中某个点的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com