Extract id from url using Angular2

后端 未结 6 933
有刺的猬
有刺的猬 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:53

    I know I'm a bit late with a reply, but just in case you were still having problem please take a look at the Angular documentation.

    angular routing tutorial

    Look at the example from the link.

    Start by importing ActivatedRoute:

    import { ActivatedRoute } from '@angular/router';
    

    Then inject it in the constructor

    constructor(private route: ActivatedRoute) {}
    

    And in OnInit()

    ngOnInit(): void {
        const id = this.route.snapshot.paramMap.get('id');
    }
    

    and like this you don't need to worry about any Observables directly.

    Hope this helps you.

提交回复
热议问题