简体   繁体   English

没有Route参数的Angular 4查询参数

[英]Angular 4 Query Params without Route params

This works (with Route Params = :id ) 这有效(使用Route Params = :id

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

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

but without Route Params, it does'nt work, 但没有Route Params,它就不起作用,

Is there a way to make below code work? 有没有办法使下面的代码起作用?

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

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

Or any other way to achieve this? 或任何其他方式来实现这一目标?

This is my QueryParamsComponent 这是我的QueryParamsComponent

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

Angular Version: 4.2.4 角版本:4.2.4

You can define multiple routes with or without parameter calling the same component and you don't need to put your queryParams in your routes config: 您可以定义带或不带参数调用同一组件的多个路由,而无需将queryParams放入路由配置中:

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

and inside your component : 在组件内部:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM