The time has come! Let's implement some routing features. In this exercise we're going to route to components.
In order to route from our contacts list to a contact detail page, the first thing we need to do is to extract the current contacts list implementation into its own component, so that ContactsAppComponent is just a "frame" that holds our app, consisting of multiple components, together.
We can create a new component ContactsListComponent using angular-cli by running:
$ ng generate component contacts-list
Or the shorter version:
$ ng g c contacts-list
- Create a component
ContactsListComponentwith angular-cli and move the current contact list logic there fromContactsAppComponent - Import
ContactsListComponentinsrc/app/app.module.tsand add it to the module'sdeclarations. - Create
src/app/app.routes.ts, import theContactsListComponentand export an array with routes (e.g.export const APP_ROUTES = [...]), holding one route pointing toContactsListComponent. - In
app.module.tsimportRouterModulefrom@angular/routerandAPP_ROUTESfrom./app.routes - Add
RouterModule.forRoot(APP_ROUTES)to theimportsarray of the module - Add
<router-outlet>inContactsAppComponent's view to load and render components
- Figure out how to create a route that redirects to
/when none of the configured routes are matched by the router.