如何从Angular 5中的paramMap获取查询参数?

时间:2018-01-07 23:52:45

标签: angular

如何从Angular 5中的paramMap获取查询参数?

此代码无法正常工作,因为我试图将值分配给void。我想在依赖于id的switchmap中链接其他调用。

    this.route.paramMap
    .switchMap((params: ParamMap) =>
        console.log(params.get('id'));

1 个答案:

答案 0 :(得分:0)

如果您的路线看起来像这样

{path: 'foo/:fid', component: fooDetailsComponent}

然后在你的组件中

export class PopDetailsComponent implements OnInit, OnDestroy {
private subscription: Subscription;
constructor(private actRoute: ActivatedRoute){
this.subscription = this.actRoute.params.subscribe(
  (params: any) => {
    if (params.hasOwnProperty('fid') && params['fid'] != '') {
      this.fooId = params['fid'];
  });
}
ngOnDestroy() {
  if(this.subscription) {
    this.subscription.unsubscribe();
  }
 }
}