Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current route

The current docs only talk about getting route params, not the actual route segments.

For example, if i want to find the parent of current route, how is that possible?

like image 880
pdeva Avatar asked Jan 04 '16 18:01

pdeva


People also ask

How do I find my current route URL?

There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service.

How do you find the current route in react?

Use the useLocation() hook to get the current route with React Router, e.g. const location = useLocation() . The hook returns the current location object. For example, you can access the pathname as location. pathname .

How do you get the current route in flutter?

When a button in the bottom bar is clicked, its current route path (/a/b/c) is saved and previously saved route is restored according to the button click. Conceptually user will think each button as a workspace and its state is never get lost (including back stack).


2 Answers

The new V3 router has a url property.

this.router.url === '/login' 
like image 170
Victor96 Avatar answered Sep 21 '22 12:09

Victor96


Angular RC4:

You can import Router from @angular/router

Then inject it:

constructor(private router: Router ) {  } 

Then call it's URL parameter:

console.log(this.router.url); //  /routename 
like image 22
lowcrawler Avatar answered Sep 25 '22 12:09

lowcrawler