AI Hints
Home›Angular Tutorial›Lazy Loading, Route Guards and Practice: Book Pages
Angular · Lesson 81 of 120

Practice: Book Pages

Your turn.

⏱ 2 min read▶ Video at 1:36:58🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Let's practise routing. You'll set up the routes for the shop's book pages.

  • Book pages
  • Routes
  • Your turn

Book page routes

Your turn. Pause the video, and try this. Write the routes. The bare address redirects to books. Books shows the list, and books slash id shows one book, loaded lazily. Anything else shows not found. Here is one solution. The redirect comes first. The detail page uses load component. And the two stars come last. Clicking Persuasion opened books slash five.

✍️ Exercise: Book page routes

Routes: '' redirects to 'books'; 'books' shows BookList; 'books/:id' lazy-loads BookDetail; '**' shows NotFound.

Show hint

{ path: 'books/:id', loadComponent: () => import('./book-detail').then(m => m.BookDetail) }

Show solution
app.routes.tstypescript
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { NotFound } from './not-found';

export const routes: Routes = [
  { path: '', redirectTo: 'books', pathMatch: 'full' },
  { path: 'books', component: BookList },
  {
    path: 'books/:id',
    loadComponent: () => import('./book-detail').then(m => m.BookDetail),
  },
  { path: '**', component: NotFound },
];

What module nine taught

Routes link paths to components, shown in the router outlet. Router link, and the router service, move between pages. Parameters become inputs. And redirects, not found pages, lazy loading, and guards complete the picture.

keyvalue
routes + <router-outlet />pages
routerLink, navigate()moving around
books/:idparameters
loadComponent, canActivatelazy and guarded

Summary

Your app now has real pages, with addresses. That's the end of module nine. Next, forms.

Key takeaways
  • Routes
  • Parameters
  • Guards
Next up: Forms in Angular — collect input