angular - 我可以在 Angular2 中执行此操作吗(子路由中的子路由)
问题描述
我正在使用 Angular 构建一个应用程序,我突然想到我可以构建一系列女儿/孙女路由,在实现它时,孙女级别不起作用,我的问题是这是否可以完成并且我实现它错误或如果不可能这样做。
主要路线是通过延迟加载加载的,然后是一个路由 ts 文件,后面是主要路线的女儿(下面示例中的第一级),在我想放置孙女的那个区域内,但它从来没有用过,尽管url 没有改变,我只是没见过孙子组件。
const routes:Routes = [
{
path: '',
children: [
{
path:'route1',
component:Component1,
children:[
{
path:'son-component-1',
component:SonComponent1
},
{
path:'other-son-component-1',
component:OtherSonComponent1,
children:[
{
path:'grandson-component-1',
component: GrandsonComponent1
},
{
path:'other-grandson-component-1',
component: OtherGrandsonComponent1
}
]
},
{
path:'sonNComponent1',
component:SonNComponent1
},
]
},
{ path:'', redirectTo: 'home' },
{ path:'**', redirectTo: 'home' }
]
}
];
非常感谢您的意见。问候。
解决方案
每个带有子组件的父组件都需要<router-outlet></router-outlet>
在组件 html 文件中添加一个。
在下面的示例中,ParentComponent 和 Child2Component 都需要一个路由器插座。还使用 path:{'', redirectTo: 'path', pathMatch: 'full'}
自动加载您希望用户加载的路径,如果它没有由 url 指定。
const routes: Routes = {
{ path: '', component: ParentComponent, children: [
{ path: '', redirectTo: 'child1', pathMatch: 'full'},
{ path: 'child1', component: Child1Component },
{ path: 'child2', component: Child2Component, children: [
{ path: '', redirectTo: 'grandson1', pathMatch: 'full' },
{ path: 'grandson1', component: Grandson1Component },
{ path: 'grandson2', component: Grandson2Component }
]}
]
}
例如,如果 Parent 组件是包含 sidenav 的主页面,并且对于主组件,它显示 Child1 和 Child2 组件,这很有用。并且如果 Child2 组件有一个选项卡视图来显示不同的内容,其中内容在每个孙子组件中,并且 child 2 包含选项卡处理。
如果您希望每条路由与同一个路由器出口相关(例如,视图中没有树),您需要使路由平坦:
const routes: Routes = {
{ path: '', component: ParentComponent, children: [
{ path: '', redirectTo: 'child1', pathMatch: 'full' },
{ path: 'child1', component: Child1Component },
{ path: 'child2', redirectTo: 'child2/grandson1', pathMatch: 'full'},
{ path: 'child2/grandson1', component: Grandson1Component },
{ path: 'child2/grandson2', component: Grandson2Component },
{ path: 'child3, component: Child3Component}
]}
}
这里只需要 ParentComponent 中的 router-outlet。如果您想对所有路线使用相同的视图区域,这将非常有用。
奖金:
const routes: Routes = {
{ path: '', component: ParentComponent, children: [
{ path: '', redirectTo: 'child1', pathMatch: 'full' },
{ path: 'child1', component: Child1Component },
{ path: 'child2', redirectTo: 'child2/grandson1', pathMatch: 'full'},
{ path: 'child2/grandson1', component: Grandson1Component },
{ path: 'child2/grandson2', component: Grandson2Component },
{ path: 'child2/:id', component: 'GrandsonWithUrlParameterComponent' }, // 'bonus'
{ path: 'child3, component: Child3Component }
]}
}
在 GrandsonWithUrlParameterComponent 中,您可以显示来自数据库的不同数据,由同一组件中的 url 指定。例如,id 可以是 db 项目的 id。
您可以使用 :id 访问组件中的内容
export class GrandsonWithUrlParameterComponent implements OnInit {
constructor(private route: ActivatedRoute) {
}
ngOnInit() {
const id = this.route.snapshot.paramMap.get('id'); // f.example the id of a db item
}
}
推荐阅读
- azure - Is there a way to use the TimeSeries Insights graphs in your website?
- c# - 有什么方法可以检测到返回类型为 void 的 flood_fill 递归方法的结束?
- node.js - MERN Stack: User ID returns null on POST
- c - 关于二维矩阵线性化的问题
- android - 如何通过 ViewModel 从 Fragment 观察 Repository LiveData
- typo3 - TYPO3:为 1 页修改 TSConfig 不适用于 cs_seo
- mysql - MySQL 高级排序结合两个查询
- node.js - 如何用凹度绘制文字
- python - 每当我导入 beautifulsoup4 时,它都会显示模块对象不可调用。我不知道为什么?
- apache-spark - Structured Streaming 是如何为每个微批次规划流式查询的逻辑计划的?