Search code examples
angularrouterangular-activatedroute

How to get Router params when using pipe in combination with skipWhile?


I have a dynamic BreadCrumb navigation where I need to get the params from URL. To achieve this, I'am executing the following code:

const userId = this.activatedRoute.params
               .pipe(skipWhile((params: Params) => Number.isNaN(+params['userId'])));

const getAppUserId = userId.pipe(
            switchMap((params: Params) => {
                return +params['userId']
                    ? this.myAppService.getApplicationUser(+params['userId'])
                    : of('')
            }))

But it's always empty and I never get the params passed to the Observable. When I use queryParams, it works fine, but I don't want to go for queryParams concept. Any idea where I'am going wrong?


Solution

  • Issue resolved. Was kind of simple...the fix was to catch the param from route.snapshot.

    this.router.events
            .pipe(filter(event => event instanceof NavigationEnd))
            .pipe(map(() => {return this.activatedRoute;}))
            .pipe(
                map((route) => {
                    while (route.firstChild) {route = route.firstChild;}
                    return route;
                })
            ).subscribe(route => {
                 console.log(`param: ${route.snapshot.params['id']}`);
                 ...
                 ...
            });