Reputation: 1095
I'm following the official Angular 2 tutorial and it uses the navigate function in a very similar way as the routerLink. What is the difference between them? Which to use when?
this.router.navigate(['/detail', this.selectedHero.id]);
[routerLink]="['/detail', hero.id]"
Upvotes: 10
Views: 17291
Reputation: 1
routerLink
is a directive that behind the scenes uses router.navigate()
as well.
The value of routerLink
and the function-argument in navigate
is exactly the same.
Upvotes: 0
Reputation: 17879
[routerLink]
is an Angular directive that you can use in html for <a>
or <button>
elements. The directive will listen for click events and navigate to the path created with params
provided to routerLink
.
this.router.navigate(['/detail', this.selectedHero.id]);
is used in your component or service to navigate.
routerLink
directive and this.router.navigate
should navigate to exactly the same url if the same arguments are specified.
Upvotes: 13