Angular 4 个没有路由参数的查询参数

Angular 4 Query Params without Route params

这有效(路由参数 = :id

{
    path: 'qp/:id?repo=1',
    component: QueryParamsComponent
}

<li><a [routerLink]="['/qp', 5]" [queryParams]="{repo:1}">Query Params</a></li>

但是没有路由参数,它不起作用,

有没有办法让下面的代码工作?

{
    path: 'qp?repo=1',
    component: QueryParamsComponent
}

<li><a [routerLink]="['/qp']" [queryParams]="{repo:1}">Query Params</a></li>

或任何其他方式来实现这一目标?

这是我的 QueryParamsComponent

repo = ' ';
ngOnInit() {
  this.router.params.subscribe((params: Params) => {
    this.repo = params['repo'];
  });
}

Angular版本:4.2.4

您可以定义多个路由,带或不带参数调用同一个组件,您不需要将 queryParams 放在路由配置中:

{
 path: 'qp/:id,
 component: QueryParamsComponent
},
{
  path: 'qp',
  component: QueryParamsComponent
}

在你的组件中:

this.router.queryParams.subscribe(params => {
    this.repo= +params['repos'];
 })