I have a scenario where search can be performed only either of one value (id or age or sex).
If i use below route where only id is passed,
this.router.navigate(['details'],
{
queryParams:
{
id: this.id,
age: this.age,
sex: this.sex,
}
})
url will look like http://path/details?id=10&age=&sex=
I want my url to be like
path/details?id=10 on id search
path/details?age=25 on age search
So i refactored my code to,
if(this.id) {
this.router.navigate(['details'],
{
queryParams:
{
id: this.id
}
})
}else if(this.age){
this.router.navigate(['details'],
{
queryParams:
{
age: this.age
}
})
}else if(this.sex){
this.router.navigate(['details'],
{
queryParams:
{
sex: this.sex
}
})
}
Is there better way to refactor the code to avoid if statement