angular2 routing - Angular 2 v3 router - how to get parent route parameters in child route -


i have route defined /abc/:id/xyz

where abc/:id resolves componenta , /xyz child component displayed in router-outlet (componentb)

when navigating /abc/:id/xyz, when this.route.params.subscribe(...) (where route activatedroute) in componenta see :id. when doing same thing in componentb not see :id. guessing activatedroute using url segments.

is there anyway parameters in route?

untested, according savkin's blog, put following child parent component's router parameters:

constructor(activatedroute: activatedroute, router: router) {     const parentactivatedroute = router.routerstate.parent(activatedroute);     this.id = parentactivatedroute.params.map(routeparams => routeparams.id); } 

comparing code in router guide, need use asyncpipe id in template, or can use subscribe()... or can use snapshot if sure id value won't change:

this.id = parentactivatedroute.snapshot.params['id']; 

@chris mentions in comment below: this.router.routerstate.pathfromroot(this.route) return array of activated routes down current route. can parameters of of activated routes.

update: routerstate.pathfromroot(someroute) marked deprecated in master. in rc.5, use activatedroute.pathfromroot().


Comments