Navigating with the Router service

The companion to using routerLink inside the template to navigate is doing it from inside JavaScript. Angular exposes the navigate() method from inside a service, which allows you to accomplish exactly this.

Note

The code, links, and a live example of this are available at http://ngcookbook.herokuapp.com/8004/.

Getting ready

Begin with the application that exists at the end of the How to do it... section of the Navigating with routerLinks recipe.

Your goal is to add an additional route accompanied by a component to this application; also, you wish to be able to navigate between them using links.

How to do it...

Instead of using routerLink, which is the most sensible choice in this situation, you can also trigger a navigation ...

Get Angular 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.