Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to get Current Route Path Name in Angular?

I was looking for a good way to get the current route's path name. This was the easiest I could find.

this.route.snapshot.firstChild.url[0].path 

Is there a better way? Thanks!

like image 221
jaruesink Avatar asked Apr 12 '17 04:04

jaruesink


People also ask

How do I name my current route?

Accessing The Current Route The Route::current() method will return the route handling the current HTTP request, allowing you to inspect the full Illuminate\Routing\Route instance: $route = Route::current(); $name = $route->getName(); $actionName = $route->getActionName();

How do you find the current path in Angular 6?

Steps to get current route URL in Angular. Import Router,NavigationEnd from '@angular/router' and inject in the constructor. Subscribe to the NavigationEnd event of the router. Get the current route url by accessing NavigationEnd's url property.

What does the * * path in Angular router do?

The two asterisks, ** , indicate to Angular that this routes definition is a wildcard route. For the component property, you can define any component in your application.

Why ActivatedRoute is used in Angular?

ActivatedRoute Contains the information about a route associated with a component loaded in an outlet. It can also be used to pass data from one component to another component using route such as Id, flag, state etc.


Video Answer


2 Answers

Thanks everyone for the answers. Here is what I found that I had to do.

router.events.subscribe((event: Event) => {   console.log(event);   if (event instanceof NavigationEnd ) {     this.currentUrl = event.url;   } }); 
like image 172
jaruesink Avatar answered Sep 19 '22 23:09

jaruesink


easiest way to find current path is either you directly use

this.router.url 

or you can check current path on every router change using its events like this

this.router.events.subscribe((res) => {      console.log(this.router.url,"Current URL"); }) 

where router here is instance created in constructor like this

constructor(private router: Router){ ... } 
like image 29
Pardeep Jain Avatar answered Sep 20 '22 23:09

Pardeep Jain