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

后端 未结 5 867
终归单人心
终归单人心 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:09

    Try this:

    Step-1: In routing module

    { path: 'product/:id', component: ProductDetailComponent }
    

    Step-2: Send the value to routing

    Home //say, id=5
    

    Step-3: Read the value in ProductDetailComponent

    First inject ActivatedRoute from '@angular/router and say route is the injected service. Use the below code inside ngOnInit() method to read it.

    id = this.route.snapshot.paramMap.get('id');
    

提交回复
热议问题