Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only path from full url string

When using Router ( this.router.url) I am get the string /MyRouterName?type=abc, sometime /MyRouterName if not have parameter

Can I only get the path /MyRouterName (for all cases) from the full url?

like image 792
Phan Hoàng Nhân Avatar asked Jul 15 '16 08:07

Phan Hoàng Nhân


People also ask

How do I find the path of a URL?

The getPath() function is a part of URL class. The function getPath() returns the Path name of a specified URL.

How do I read a full URL?

You'll see “https://www.howtogeek.com” instead of “howtogeek.com”. Just right-click in Chrome's address bar select “Always show full URLs” to make Chrome show full URLs.

How do you split a URL in node?

You can split the url string using the split() method by specifying a delimiter ( / in your case) and it will return an array.


1 Answers

You can use the activated route component's snapshot to get this url.

    import { Router, ActivatedRoute } from '@angular/router';      constructor(         private router: Router,         private activatedRoute: ActivatedRoute) {          console.log("routes");         console.log(activatedRoute.snapshot.url); // array of states         console.log(activatedRoute.snapshot.url[0].path);      } 

Link to original SO answer by Jose G Varanam How to get current route

like image 93
user5049376 Avatar answered Sep 28 '22 16:09

user5049376