How to get angular 5 route segments from ActivatedRoute?

ActivatedRoute provided a member pathFromRoot which let developers to get full activedroutes from root, refer to the docs.

For your situation, you can use below code block to achieve it:

this.activatedRoute.pathFromRoot[1].url.subscribe(val => console.log(val[0].path));

Mention that currently the result of pathFromRoot contains a empty route(first element of the ActivatedRoute[]) which means for the path ''.


You can do it this way :

let url = this.activatedRoute.snapshot.url.join().split(',')
console.log(url[0]); 

If you want to retrieve the parent route, then have a look here.

Tags:

Angular