IT

[angular] Router navigate relatively

로바아토 2018. 3. 19. 19:40

we know this last post,

this.router.navigate(['/servers']);


this is absolute path. but funny thing is this code.


this.router.navigate(['servers']);

this looks like relative path. but it is not,


routerLink in html acts relatively with ['servers'] but navigate is not.

(do not wonder why. just it works like that.) 


if you want to navigate relatively use this.

this.router.navigate(['servers'], {relativeTo: this.route});


route can be declared by this.

constructor(private serversService: ServersService,
private router:Router,
private route:ActivatedRoute) { }