Javascript Angular2如何执行url查询字符串

Javascript Angular2如何执行url查询字符串,javascript,angular,Javascript,Angular,我的应用程序上有一个搜索功能,可以搜索产品列表,一切正常,但是我想将用户重定向到另一个页面,比如说www.site.com/search?keyword=香水 我只能做www.site.com/search/香水和www.site.com/search;关键字=香水 路由器导航this.Router.navigate(['search',{keyword:searchVal}])以www.site.com/search的方式执行此结果;关键字=香水 路线 const appRoutes: Rou

我的应用程序上有一个搜索功能,可以搜索产品列表,一切正常,但是我想将用户重定向到另一个页面,比如说
www.site.com/search?keyword=香水

我只能做
www.site.com/search/香水
www.site.com/search;关键字=香水

路由器导航
this.Router.navigate(['search',{keyword:searchVal}])
www.site.com/search的方式执行此结果;关键字=香水

路线

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

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


如何将我的url设置为
www.site.com/search?关键字=香水

您应该按照以下方式更新您的批准:

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

您应按如下方式更新您的批准:

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

您应该使用
queryParams

this.router.navigate(['/search'], { queryParams: { keyword: searchVal } });

您应该使用
queryParams

this.router.navigate(['/search'], { queryParams: { keyword: searchVal } });

哦,谢谢,我想我得把那个情人放进去。这起作用了:)哦,谢谢,我想我得把那个情人放进去。这起作用:)这将输出一个参数
/search/product
而不是一个查询字符串
/search?keyword=product
这将输出一个参数
/search/product
而不是一个查询字符串
/search?keyword=product