How to pass and get parameter with routerLink in Angular 2?

后端 未结 5 860
终归单人心
终归单人心 2021-02-06 06:30

I want to pass a value with url using routerLink. And read that value on another page. Like I have product list. On select of first record the id of that record pass to product

5条回答
  •  失恋的感觉
    2021-02-06 07:16

    Use routerLink on your a tag for passing it by url.

    [routerLink]="['yourRouteHere', {'paramKey': paramValue}]
    

    To get it you need to use ActivatedRoute service. Inject it into your component and use it's subscribe method. Here my route is the injected service

    this.route.params.subscribe(params => {
       const id = Number.parseInt(params['paramKey']);
    }
    

    If you want to get parameters from the route segment use .params, else if you want from query string, use .queryParams

提交回复
热议问题