【发布时间】:2018-03-07 04:23:23
【问题描述】:
我想在单击搜索按钮时在我的 URL 中传递一个参数。 我希望它看起来像这样。 /result?search=sap
但是,我在控制台中收到此错误。 错误:无法匹配任何路由。 URL 段:'结果;搜索 = sap' 我无法弄清楚我做错了什么。这是我的代码。
SearchComponent.ts
search(){
//this function is accessed when a user clicks on a search button in the HTML
this.router.navigate(['/result', { search: this.query }]);
//this.query = sap
}
AppModule.ts
const routes: Routes = [
//other routes here
{ path: 'result/:search', component: SearchComponent}
];
@NgModule({
declarations: [
//other components
SearchComponent
] ,
imports: [
//other modules
RouterModule.forRoot(routes, {}),
],
【问题讨论】:
-
您在
this.router.navigate中混合了必需的参数语法(与路由上的:search)和可选参数语法。请参阅这篇文章,其中概述了哪种语法与哪种路由参数类型:stackoverflow.com/questions/44864303/…
标签: angular angular5 angular-router