As of Angular 5.2, you can do Router configuration to inherit all params to child states. See this commit if interested in the gory details, but here's how it's working for me:
Wherever you have your call to RouterModule.forRoot()
, include a configuration object with the inheritance strategy set to always
(default is emptyOnly
):
import {RouterModule, ExtraOptions} from "@angular/router";
export const routingConfiguration: ExtraOptions = {
paramsInheritanceStrategy: 'always'
};
export const Routing = RouterModule.forRoot(routes, routingConfiguration);
Now when you're in a child component looking at a ActivatedRoute
, ancestors' params appear there (e.g. activatedRoute.params
) rather than something messy like activatedRoute.parent.parent.parent.params
. You could access the value directly (e.g. activatedRoute.params.value.userId
) or subscribe via activatedRoute.params.subscribe(...)
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…