0

I have a url of this form

http://localhost:8100/my-child/2

When when i click a button i am getting result as

http://localhost:8100/my-child/2/5

How to adjust the url to fit to this form as

http://localhost:8100/my-child/5

This is my code snippet

  this.href = this.router.url;
   console.log(this.router.url);
   this.router.navigateByUrl(this.router.url+id);

Can you please tell me how to adjust the code to get the required output.

2
  • Assuming that 2 and 5 are both ids of my-child, you must have you route defined for when you enter my-child/2. Use the same route, pass different param Commented Feb 13, 2020 at 4:59
  • thaks Juilius. I shall try that. Commented Feb 13, 2020 at 5:46

1 Answer 1

3

You should use route params:

export const routes: Routes = [
  { path: '', redirectTo: 'product-list', pathMatch: 'full' }
  { path: 'product-details/:id', component: ProductDetails }
];

Code to navigate to the route:

goToProductDetails(id) {
  this.router.navigate(['/product-details', id]);
}
Sign up to request clarification or add additional context in comments.

Comments

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.