5

I know this has been asked many times and I have tried to look for a solution.

My current URL is: http://localhost:4200/configuration

Below is one of the many online found solutions I am trying to implement.

export class AppComponent implements OnInit{
  title = 'app';
  constructor(private router: Router, private activatedRoute: ActivatedRoute) { 

  }

  ngOnInit() { 
    console.log("On Init");
    console.log(this.router.url);
    console.log(this.activatedRoute.url);
    //this.router.navigate(['/login']);
  }
}

On reload of page I get below output.

On Init 
\

I am just getting empty url. I am very curious to know what I am missing.

6
  • what do you expect to output? Commented Sep 4, 2017 at 14:41
  • shouldn't I get /configuration ?? Commented Sep 4, 2017 at 14:42
  • show your routes confguratoin Commented Sep 4, 2017 at 14:43
  • do you only want to get '/configuration ? Commented Sep 4, 2017 at 14:47
  • @Faisal yes if possible. Commented Sep 4, 2017 at 15:05

3 Answers 3

11

You can do it using location.href. Another option is to use DOCUMENT from '@angular/platform-browser':

import { DOCUMENT } from '@angular/platform-browser';

constructor(@Inject(DOCUMENT) private document: any){
    console.log(location.pathname);
    console.log(location.href);
    console.log(this.document.location.href);
}

If you only want to get "/configuration" from your url, then use location.pathname.

Demo here.

Sign up to request clarification or add additional context in comments.

6 Comments

This is from Javascript. Is there no way to do this right in angular?
this is angular. Have you seen the demo ?
its kind of a hack. i did it in a batter way but have refer once in office tomorrow
Why add the overhead of Angular if you can do something simply in JavaScript. That's a silly constraint to this Answer.
Very much aware of that, but Angular is a JavaScript framework therefore a JavaScript solution is also an Angular solution by proxy.
|
2

This will give "/configuration"

import { Router, NavigationEnd } from '@angular/router';
import { isPlatformBrowser } from '@angular/common';

....

constructor(
    private router: Router,
....

ngOnInit() {
    if (isPlatformBrowser) {
      this.router.events
        .filter(event => event instanceof NavigationEnd)
        .subscribe(() => {
          console.log(this.router.routerState.snapshot.url);
        })
    }
  }

or

  ngOnInit(){
    console.log(this.router.url)
  }

2 Comments

Works for me, by removing the "filter" line: if (isPlatformBrowser) { this.router.events.subscribe(() => { console.log(this.router.routerState.snapshot.url); }); }
@MiguelLara, just this.router.url gives the same result :)
0

You are accessing the property correctly. However, you're accessing it before it's set. See:

https://stackblitz.com/edit/angular-8phmvc

If you check the browser log you can see which part of each component's lifecycle has it set.

But long story short, instead of ngOnInit try ngAfterViewChecked or look for another way to subscribe to the current location using router.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.