routing - 角度多级子路由
问题描述
我有以下按模块分组的配置路由:
在上面:
imports: [
ContentModule,
RouterModule.forRoot([
{ path: 'Home', component: HomeComponent },
{ path: '', component: HomeComponent, pathMatch: 'full' }
])
下一个模块(ContentModule):
imports: [
ApplicationsModule,
RouterModule.forChild([
{
path: 'Content', component: ContentComponent, canActivate: [AuthGuardService], children:
[
{ path: 'Apps/List', component: ApplicationListComponent, canActivate: [AuthGuardService], resolve: { applications: ApplicationListResolver } },
{ path: 'Apps/Details/:applicationID', component: ApplicationTabContainerComponent, canActivate: [AuthGuardService] },
{ path: 'Apps/NewApp', component: NewApplication, canActivate: [AuthGuardService] },
{ path: 'Documentation', component: DocComponent },
{ path: '', redirectTo: 'App/List', pathMatch: 'full' },
{ path: '**', redirectTo: 'App/List' }
]
}
])
最后一个模块(ApplicationsModule):
imports: [
RouterModule.forChild([
{ path: 'info', component: ApplicationDetailComponent, canActivate: [AuthGuardService], resolve: { application: ApplicationDetailResolver } },
{ path: 'CDN', component: FileManager, canActivate: [AuthGuardService], resolve: { fileStorageData: FileManagerResolver } },
{ path: '', redirectTo: 'info', pathMatch: 'full' }
])
好吧,如果我去http://localhost:8253/Content/Apps/Details/Web-Site-App/info我收到以下错误:
无法匹配任何路由。URL 段:'Content/Apps/Details/Portal-Web-Drupal/inf
任何人都知道我为什么会收到这个错误?
我以这种方式构建了模块,因为 ContentComponent 在第一个路由器出口上呈现(它具有导航栏(它是静态内容)和 ContentComponent)。ContentComponent 有一个路由器出口,用于渲染 DocumentationComponent 或 ApplicationListComponent 或 NewApplicationComponent 或 ApplicationTabContainerComponent。ApplicationTabContainerComponent 有一个可以渲染 ApplicationDetailComponent 或 FileManagerComponent 的路由器出口。
也许,问题是我不能有超过两层的路由器插座,我有三个:
- 祖父母是应用程序的路由器出口,它呈现内容组件
- parent (ContentComponent) 是渲染 applicationList 或 newApplication 或 applicationTabContainer 的路由器出口
- 子组件是在 ApplicationTabContainerComponent 路由器出口中呈现的组件。
解决方案
要建立层次结构,您需要两件事:
1)父组件中的路由器出口(听起来像你有)。
2)父路由的children
属性(当路由在模块中定义时)或loadChildren
属性(当路由在单独的模块中定义时)让路由器知道哪些路由是子路由。
ApplicationTabContainerComponent 缺少children
or loadChildren
route 属性。否则,没有任何东西可以将 Applications 模块中定义的路由绑定到该路由。
在没有代码的情况下获得正确的语法有点挑战性......但它看起来像这样:
imports: [
RouterModule.forChild([
{
path: 'Content', component: ContentComponent, canActivate: [AuthGuardService], children:
[
{ path: 'Apps/List', component: ApplicationListComponent, canActivate: [AuthGuardService], resolve: { applications: ApplicationListResolver } },
{
path: 'Apps/Details/:applicationID',
component: ApplicationTabContainerComponent,
canActivate: [AuthGuardService],
loadChildren: './yourpath/applications.module#ApplicationsModule`
},
{ path: 'Apps/NewApp', component: NewApplication, canActivate: [AuthGuardService] },
{ path: 'Documentation', component: DocComponent },
{ path: '', redirectTo: 'App/List', pathMatch: 'full' },
{ path: '**', redirectTo: 'App/List' }
]
}
])
这种语法可能不完全正确,但应该给你的想法。(我只loadChildren
在无组件路线上使用过。)
推荐阅读
- javascript - “classList.add”不会将类添加到 div 和表单元素
- amazon-s3 - 使用 S3Hook 获取 S3 文件的最后修改日期
- javascript - 寻找有关如何使用 Handlebars 在窗口中显示实时控制台输出的一般指针
- asp.net-core - 在我的整个 VS 2019 解决方案中更新 System.Text.Encodings.Web 的正确方法是什么?
- haskell - Haskell Core 类型定义中的 AppTy 和 FunTy 有什么区别?
- azure-devops - 使用 Azure DevOps 为 GCP BigQuery 创建 CI/CD 管道的最佳方法
- reactjs - 在反应中使用类切换 div
- node.js - nodejs child_process spawn在Windows上静默失败
- java - 使用令牌通过注册的应用程序访问 Azure Blob
- python - SciPy 的 dblquad 需要什么格式(向量、网格网格或其他格式)进行 2D 集成?