Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current route without parameters

I need to get my current route without params in Angular 2, I found a way to get the current route with params as follows:

 this.router.url 

and then split it:

 this.router.url.split(';')[0] 

But this looks as workaround, I think there should be better way?

like image 642
Sabri Aziri Avatar asked Feb 28 '17 09:02

Sabri Aziri


People also ask

Why is my MVC route without Param not working?

This is because MVC starts searching routes in RouteConfig file from top to bottom. As soon as it finds any matching route it will stop searching further. So for your problem when there will be route without param it will pick "Withoutparam" route. As I can understand from your comment below. You need url without id in every condition.

How to get the current URL without any parameters in JavaScript?

There are multiple ways to Get the URL without any parameters in JavaScript. First get the get current URL get rid of the query parameters. const url = window.location.href.split ('?') [0] Second concat origin and pathname, if there’s present a port such as example.com:80, that will be included as well.

How to get current route without query parameters in Laravel?

To get current route without query parameters, you can use below mentioned single line: this.router.url.split ('?') [0] Show activity on this post. parseTree from Router helps fetching the segments without any knowledge about url structure.

How to get the route path after the base URL?

window.location.pathname will give you the route path (after the base URL), in our test case /somedir/somefile Using hardcoded URLs for example.


2 Answers

To get current route without query parameters, you can use below mentioned single line:

this.router.url.split('?')[0]  
like image 53
Yanusha Cooray Avatar answered Sep 22 '22 14:09

Yanusha Cooray


parseTree from Router helps fetching the segments without any knowledge about url structure.

import { Router } from '@angular/router'; ... constructor(private router: Router) {} ... const urlTree = this.router.parseUrl(url); const urlWithoutParams = urlTree.root.children['primary'].segments.map(it => it.path).join('/'); 

Start from here. If you have secondary outlets adjust as required.

like image 29
André Werlang Avatar answered Sep 19 '22 14:09

André Werlang