Full routerLink URL

From angular docs :

The router's link directive always treats the provided input as a delta to the current URL.

For example, if the current URL is /user/(box//aux:team) . Then the following link <a [routerLink]="['/user/jim']">Jim</a> will generate the link /user/(jim//aux:team) .

So how do you create a link to /user/jim ?

I tried ['/user/jim', { outlets: { aux: null }}] but this does not work.
Even if it worked, it would not be the best solution. I'm more likely looking for a way to go to an absolute URL than canceling the exit points.

EDIT:

For the same question, I have a link to the root: routerLink="/" , which does just that, it redirects to the root of my application without any dots, such as an absolute link. What's funny here, I don’t really want this behavior for this particular link, while preserving any route routes, to be fine ...

+16
source share
4 answers

As of August 2018, angular docs say:

  • If the first segment starts with /, the router will look for a route from the root of the application.
  • If the first segment starts with. / Or does not start with a slash, the router will instead look for children of the current activated route.
  • And if the first segment starts with .. /, the router will go up one level.
+15
source

You can use relative paths:

 <a [routerLink]="['./']" 

Your top parent will be redirected ( /user/jim )

 <a [routerLink]="['']" <a [routerLink]="['/']" 

Will be redirected to '/'

+9
source

A workaround for your problem might be to put a click event on your html element, then you can pass your URL to an event handler and redirect it anywhere using the "Router" imported from "@ angular / router":

 <a (click)="reRoute(urlToRoute)"></a> 

then in the .ts file :.

  import { Router} from '@angular/router'; .. constructor(private router: Router, ) { } .. reroute(url) { this.router.navigate([url]) } 

Hope this solves your problem!

0
source

I believe the easiest way to do this would be

 <a [routerLink]=['/path/you/want', varibleToBeUsed]></a> 

example

 let link.path = 'something'; 
 <a [routerLink]="['/',link.path]">test</a> <!-- www.test.com/something --> <a [routerLink]="['/user/jim',link.path]">test2</a> <!-- this will append to the base path ie www.test.com/user/jim/something --> <a [routerLink]="['jim',link.path]">test3</a> <!-- this will append to current url path ie if your current path is www.test.com/login then it will append as www.test.com/login/jim/something --> 
0
source

Source: https://habr.com/ru/post/1257682/


All Articles