Search code examples
angularangular-routing

Angular routes empty when checked


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