4

Currently I'm using this routing setup for my app:

{
  path: ':group',
  component: ArticleListComponent,
  children: [{
      path: ':subgroup',
      component: ArticleListComponent,
      children: [{
          path: ':chapter',
          component: ArticleListComponent,
          children: [{
              path: ':section',
              component: ArticleListComponent,
              children: [
              ]
            }
          ]
        }
      ]
    }
  ]
},

I use the same component for listing articles because the template and code will not change, but I want to use the url for filtering these articles. The app should work with routing params instead of get params. With these params I want to call an API to get the articles which belong to this url: /group1/subgroup1/chapter1/section1.

The problem is that I only get these params with the following code:

const params = this.route.snapshot.params;
// {group: "group1"}

const params = this.route.parent.snapshot.params;
// {}

const params = this.route.firstChild.snapshot.params;
// {group: "group1", subgroup: "subgroup1"}

How can I get all these params at once?

2 Answers 2

0

Use query params instead of params. It will greatly simplify your routing configuration.

routing.ts

{
  path: '',
  component: ArticleListComponent
}

article-list.component.ts

constructor(route: ActivatedRoute) {
  route.queryParams.pipe(
    map(({ group, subgroup, chapter, section }) => 
      ({ group, subgroup, chapter, section }))
  ).subscribe( ... )
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know it would be easier, but it's not the best for SEO and url readability. Users can visit the following urls: - /webshop - /webshop/group1 - /webshop/group1/subgroup1 - /webshop/group1/subgroup1/chapter1 - /webshop/group1/subgroup1/chapter1/section1 It's get more specific each time.
0

If in case, you have to go with route params instead of query params, there is no direct way as such. You can use the below snippet in ArticleListComponent

routerParams: any = {};
  
  constructor(private router: ActivatedRoute) {
    this.router.params.subscribe(params => {
    this.getRouteParams(); 
    console.log(this.routerParams);  
    });
    
  }

  /**
  * API to return all the route params as key value pairs
  */
  getRouteParams() {
    let currentRouter = this.router;
    let isChildRoutesExist: boolean = currentRouter ? true : false;
    while(isChildRoutesExist) {
      currentRouter.paramMap.subscribe(params => {
        const keys = params.keys;
        for(const key of keys) {
         //Sample output : {group: "group1", subgroup: "subgroup1", chapter: "chapter1", section: "section1"}
          this.routerParams[key] = params.get(key);
        }
      });
      if(currentRouter.firstChild) {
        currentRouter = currentRouter.firstChild;
      } else {
        isChildRoutesExist = false;
      }
    }
  }

3 Comments

This almost what I need! Only 1 issue left: parameters have to be updated each time the view has been entered (clear & generate parameters again).
getRouteParams() { this.routerParams = {}; ... } However, entering a child view doesn't trigger this function.
I'm not sure what you are mentioning about entering a child view. I assume when the route params updated it is a page reload wherein params will be listened. Please add more information if otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.