如何从孩子那里获取参数URL父ID [英] How to get params url parent id from children
本文介绍了如何从孩子那里获取参数URL父ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在route.ts中有这个
I have this in my route.ts
path: 'profile/:id',
component: ProfileComponent,
canActivate: [SiteRouteGuard],
children: [
{
path: 'albums',
component: AlbumsComponent,
canActivate: [SiteRouteGuard]
},
...
]
现在在我的profile.ts中我有这个
Now in my profile.ts I got this
localhost:4200/profile/45666
,当我路由到album.ts时,这就是我的个人资料。ts
and when I route to album.ts that is chlidren of my profile.ts
localhost:4200/profile/45666/albums
现在我在我的相册中。我如何获取ID 45666?
Now I'm in my album.ts how can I get the id 45666?
我尝试使用ActivatedRoute:
I tried to use ActivatedRoute:
console.log(this._route.snapshot.params.id) >> undefined
推荐答案
您可以使用
角度/路由器
import { ActivatedRoute, Router } from "@angular/router"
public activatedRoute: ActivatedRoute
this.activatedRoute.parent.params // return observable
如果您想要静态值,则要求
if you want the static value, then ask for
this.activatedRoute.parent.snapshot.params // return static values
通过使用属性parent,您可以从父组件访问所有参数和查询
By using the attribute parent, you can access to all the parameters and queries from the parent component
这篇关于如何从孩子那里获取参数URL父ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文