Angular Basic Routing Challenge
This challenge focuses on implementing fundamental routing within an Angular application. You will create a multi-page experience where users can navigate between different views using Angular's RouterModule. This is a core concept for building any single-page application (SPA) and understanding it is crucial for developing dynamic web interfaces.
Problem Description
Your task is to set up basic routing for an Angular application. This involves defining routes that map URLs to specific Angular components, enabling navigation between these components, and handling the display of routed components.
Requirements:
- Define Routes: Create a set of routes in your
app-routing.module.tsfile. Each route should associate a URL path with a corresponding Angular component. - Create Components: You will need at least three distinct Angular components to represent different "pages" or views in your application (e.g., Home, About, Contact).
- Configure Router Outlet: In your main
AppComponent's template (app.component.html), include the<router-outlet>directive. This is where Angular will render the component associated with the current route. - Implement Navigation: Add navigation links (using
routerLinkdirective) in yourAppComponent's template or a dedicated navigation component to allow users to switch between the defined routes. - Handle Default Route: Configure a default route that loads a specific component when the application is accessed at its root URL (e.g.,
/).
Expected Behavior:
- When the application loads, the component for the default route should be displayed.
- Clicking a navigation link should change the URL in the browser's address bar and render the corresponding component within the
<router-outlet>. - Directly navigating to a defined URL (by typing it into the address bar or refreshing) should also load the correct component.
Edge Cases to Consider:
- Not Found/Wildcard Route: Implement a route to handle URLs that do not match any defined paths, typically displaying a "Page Not Found" component.
Examples
Example 1: Basic Navigation
- Components:
HomeComponent,AboutComponent - Routes:
/->HomeComponent/about->AboutComponent
Input:
- Initial application load.
- User clicks a link labeled "About".
Output:
- On load:
HomeComponentcontent is displayed. - After clicking "About":
AboutComponentcontent is displayed, URL changes to/about.
Explanation: The app-routing.module.ts is configured with these two paths, and AppComponent's template contains navigation links pointing to these paths.
Example 2: Wildcard Route
- Components:
HomeComponent,NotFoundComponent - Routes:
/->HomeComponent**(wildcard) ->NotFoundComponent
Input:
- User navigates to
/non-existent-page.
Output:
NotFoundComponentcontent is displayed.
Explanation: The wildcard route ** catches any URL that hasn't been matched by previous routes and renders the NotFoundComponent.
Constraints
- You must use Angular's built-in
RouterModule. - All routing configuration should reside in
app-routing.module.ts. - Navigation should be implemented using the
routerLinkdirective. - The
router-outletdirective must be present inapp.component.html. - You should create at least three distinct components for navigation.
Notes
- Remember to import your components into
app-routing.module.tsand configure them within theroutesarray. - The
RouterModule.forRoot()method is used to set up the root routing configuration. - Consider creating a separate component for navigation links to keep
AppComponentcleaner. - Think about the order of your routes in the configuration array, as Angular matches them sequentially. The wildcard route should generally be the last one.