Dynamically create query Params in Angular 2

2020-04-04 06:10发布

问题:

i want to achieve that the queryParams can be dynmamically passed it. For now i can set the values of the params dynamically, but not the keys

Here is my Code

onItemClick(item: FilterItem, group: FilterGroup, i: number) {

    let navigationExtras: NavigationExtras = {
      queryParams: { name : group.items[i].param }
    };
     this.router.navigate(['/search'], navigationExtras);
  }

I cannot put instead of "name" my group.name which is a string. I tried this:

 queryParams: { group.name: group.items[i].param }

But here i get syntax error. How can i put the "key" of a queryParam through an object like that?

回答1:

This should work:

 queryParams: { [group.name]: group.items[i].param }

See also Creating object with dynamic keys