0

I have a search feature on my application which searches a list of products, which is all going okay however I want to redirect the user to another page say www.site.com/search?keyword=perfume

I can only make it www.site.com/search/perfume and www.site.com/search;keyword=perfume

Router navigate this.router.navigate(['search', {keyword: searchVal}]); doing this results as www.site.com/search;keyword=perfume

Routes

const appRoutes: Routes = [
  { path: 'search', component: SearchComponent} 
];

Search function:

findproduct(searchTerm: HTMLInputElement): void {
var searchVal = searchTerm.value;
this.productService.searchproduct(searchVal)
  .subscribe(data => {
    this.router.navigate(['search', {keyword: searchVal}]);
    console.log(data)
  });

}

How can I make my url to be www.site.com/search?keyword=perfume

2 Answers 2

2

You should use queryParams:

this.router.navigate(['/search'], { queryParams: { keyword: searchVal } });
Sign up to request clarification or add additional context in comments.

1 Comment

Oh thanks, I thought I had to put the param in []. This worked :)
0

You should update your appRoutes as follow:

const appRoutes: Routes = [
  { path: 'search/:keyword', component: SearchComponent} 
];

1 Comment

This will output a parameter /search/product not a query string /search?keyword=product

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.