docs(router): Clearly document how to retrieve all params in the router tree (#40306)

This commit documents how to add a helper function which combines all the params
in the router state tree into a single object. It provides a starting point for
developers to reference if they require a more fine-tuned approach.

Fixes #11023

PR Close #40306
This commit is contained in:
Andrew Scott 2021-01-04 11:54:01 -08:00 committed by atscott
parent e05ac395fc
commit b36bece17c
1 changed files with 19 additions and 1 deletions

View File

@ -298,7 +298,25 @@ export class ActivatedRouteSnapshot {
constructor(
/** The URL segments matched by this route */
public url: UrlSegment[],
/** The matrix parameters scoped to this route */
/**
* The matrix parameters scoped to this route.
*
* You can compute all params (or data) in the router state or to get params outside
* of an activated component by traversing the `RouterState` tree as in the following
* example:
* ```
* collectRouteParams(router: Router) {
* let params = {};
* let stack: ActivatedRouteSnapshot[] = [router.routerState.snapshot.root];
* while (stack.length > 0) {
* const route = stack.pop()!;
* params = {...params, ...route.params};
* stack.push(...route.children);
* }
* return params;
* }
* ```
*/
public params: Params,
/** The query parameters shared by all the routes */
public queryParams: Params,