Extract id from url using Angular2

后端 未结 6 931
有刺的猬
有刺的猬 2021-02-03 17:57

Hi I am trying to extract the id part of the url using Angular2.

http://localhost:3000/item/187809

I have been playing around with ActiveRoute

6条回答
  •  梦谈多话
    2021-02-03 18:35

    I have faced the same problem while working with Angular 8, and solved it by:

    1. At first, import ActivatedRoute:

      import { ActivatedRoute } from '@angular/router';
    2. Then, Inject ActivatedRoute in the constructor:

      constructor(private activatedRoute: ActivatedRoute) {}
    3. Your ngOnit method will look like this.

      ngOnInit(){
           const id = this.activatedRoute.snapshot.paramMap.get('id');
       }

    I found it from the official documentation of Angular.

提交回复
热议问题