Skip to main content
Back to Journal
Angular

Building an Angular App: Part 3

Angular Routing

In Part 3, we'll implement Angular's powerful routing system to navigate between our components. Routing is essential for single-page applications as it allows users to move between views without full page reloads.

Setting Up the Router Module

Angular's Router module needs to be imported and configured with our routes. Each route maps a URL path to a component:

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'posts', component: PostsComponent },
  { path: 'posts/new', component: NewPostComponent },
  { path: 'posts/:id', component: PostDetailComponent }
];

Router Outlet

The <router-outlet> directive marks where the router should render the matched component. Place it in your app component template where you want routed content to appear.

Navigation Links

Use routerLink instead of traditional anchor tags to enable client-side navigation without page reloads. The routerLinkActive directive adds a CSS class when the link's route is active.

AngularRoutingTypeScript