Angular routes empty when checked

Issue

I am trying to get the current path of my Angular app. However, this is always returned empty.

  constructor(
      private route: ActivatedRoute
  ) {
    this.route.url.subscribe(segments => {
      var currentPath = segments[0].path;
      console.log("Current Route: ", currentPath);
    });
  }

No matter where I navigate, this is always empty:

enter image description here

What am I doing wrong here?

Solution

If you want to get current route url , you can use this code :

import { Router, ActivatedRoute } from '@angular/router';
 constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute) {
    console.log(activatedRoute.snapshot.url[0].path); 
}

If you want to get full url of web app . you should use this :

window.location.pathname

Answered By – Elvin Garibzade

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published