Charting the Path: Defining Routes and Links in Angular

Learn to define routes in your Angular application by mapping URLs to components, and create navigation using the routerLink directive and the router-outlet.

🔗

Welcome! Let's create a navigation link and the route it points to.

/* Starting navigation setup... */

The 'path' Property

The `path` property is a string that specifies the URL segment for a route. For example, a path of `'users'` will match a URL like `yourapp.com/users`. You can also define dynamic paths with parameters, like `'users/:id'`, to handle variable data.

The 'component' Property

The `component` property tells the router which component to display when the route's path is matched. The value is a reference to the component class, not a string (e.g., `component: UserListComponent`).

The 'routerLink' Directive

The `routerLink` directive is used on clickable elements (like `<a>` or `<button>`) to trigger navigation. It's superior to `href` because it uses the router's history mechanism, preventing a full page reload and providing a smoother user experience.

The '<router-outlet>' Directive

The `<router-outlet>` is a directive that acts as a placeholder in your template. Angular dynamically inserts the component corresponding to the current route into this outlet. It's the mechanism that makes your Single Page Application (SPA) work.

Practice Zone


Interactive Test 1: Define a Route

Drag the value to its correct property in a route definition.

Arrastra en el orden correspondiente.


Arrastra las opciones:

'products'
ProductListComponent

Completa el código:

path:______
component:______
Unlock with Premium

Interactive Test 2: Build a Route and Link

Rellena los huecos en cada casilla.

// Route Definition
{ path: 'Dashboard', : DashboardComponent }

// Template Link
<a ="/Dashboard">Dashboard</a>
Unlock with Premium

Practice Example: Create a Link and Route

Create a link to a 'products' page in an HTML template and define the corresponding route that loads `ProductListComponent`.

* Write the code below. Correct characters will be shown in green and incorrect ones in red.

// In app.routes.ts export const routes: Routes = [ { path: 'products', component: ProductListComponent } ]; // In app.component.html <a routerLink="/products">View Products</a> <router-outlet></router-outlet>
Unlock with Premium

Knowledge Check

What is the purpose of <router-outlet>?


Unlock with Premium

Advanced Navigation Techniques

Beyond basic links, Angular's router handles complex scenarios like passing parameters and redirecting users.


1. Passing Route Parameters

Dynamic routes, like `products/:id`, are common. You can create links to them using a property binding with an array. The router constructs the final URL from the array's contents.

// Link to 'products/123'
<a [routerLink]="['/products', product.id]">{{ product.name }}</a>

2. Redirecting Routes

You often need to redirect from one path to another. A common use case is redirecting the empty path `''` to a default page like `/home`. This is done with `redirectTo` and `pathMatch: 'full'`. `pathMatch: 'full'` ensures the redirect only happens if the entire URL path is empty.

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent }
];

Practical Takeaway: Use array binding with `routerLink` for dynamic routes. Use `redirectTo` to create sensible defaults and handle old URLs gracefully.

Routing Definitions Glossary

path
A string that specifies the URL segment that a route configuration should match.
component
A property in a route configuration that specifies the component class to display for that route.
routerLink
A directive for creating navigation links. It prevents full page reloads by using the router's navigation capabilities.
router-outlet
A directive that acts as a placeholder where the component for the currently active route is rendered.
redirectTo
A property in a route configuration that tells the router to navigate to a different URL.
pathMatch
Specifies the matching strategy. `'full'` means the path must match the entire URL segment. `'prefix'` (the default) means it only needs to match the beginning.