liveBook · Manning

routerLink directive

As explained in Pro Angular 16

routerLink is an Angular directive that associates an HTML element with a navigation route and navigates when the element is activated.

Use routerLink to make an element navigate to a configured route when activated. Set its value to the target URL: a button with routerLink="/cart" opens the cart route. The directive can be applied to any element, though buttons and anchors are typical choices. section 6.3.3 section 24.2.5

Import RouterModule into the feature module that contains the template. A routerLink changes the browser URL within the running application; Angular then matches the URL to a route and displays its component in a router-outlet. Typing the same URL into the address bar instead sends a request to the server and reloads the application. section 24.2.5 section 24.2.6

Cart navigation

A cart-summary button uses routerLink="/cart" and [disabled]="cart.summary().itemCount == 0". It navigates when clicked only after the cart contains a product. By contrast, an Add To Cart component method uses Router.navigateByUrl("/cart") when navigation belongs in component code. section 6.3.3

Table and form navigation

An Edit button targets /form/edit, a Create New Product button targets /form/create, and the form’s Cancel button targets /. These links let users move between the product table and form without entering URLs manually. section 24.2.5

In an administration sidenav, routerLink buttons select features displayed in a nested router-outlet. Pairing them with routerLinkActive applies styling that indicates the selected feature. section 7.5.2

How Pro Angular 16 covers it

How deep it goes
Explains attribute setup, route matching, and the difference between client-side navigation and manually entering a URL, without detailing further routing options.
Examples
Uses a disabled cart button, product-table and form buttons, and administration sidenav buttons.
What it assumes
Assumes a configured Angular route and a component template, while showing how to import RouterModule to enable routerLink.
What you can do after
Apply it via Angular RouterModule to add template navigation and distinguish it from navigation through component code.
How it connects
Links RouterModule, route configuration, router-outlet, routerLinkActive, and Router.navigateByUrl to template-driven navigation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.