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!
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();
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.
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.
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.
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; } });
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){ ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With