StartingAgain
StartingAgain

Reputation: 219

Get the URL of a Component

I am trying to get this._my.dialog = location.pathname.replace('/', ''); in Angular8 , but this does not always return the url, even if I include it in ngOnInit().

Especially, when:

app.component.html

  <app-nav-menu></app-nav-menu>
  <div class="container">
    <router-outlet></router-outlet>
    <app-myAppForGettingTheURL></app-myAppForGettingTheURL>
  </div>

How do I do it correctly?

Upvotes: 0

Views: 274

Answers (1)

Gaurang Dhorda
Gaurang Dhorda

Reputation: 3387

Complete working demo is found here.. StackBlitzLink

You need to subscribe router events in root component of your project. same like this.

 this.router.events.subscribe((event: any) => {
      if (event instanceof NavigationEnd) this.url = this.router.url
 });

Just change url from home to about and see routed url in app.component.

Upvotes: 2

Related Questions