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]"
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With