Hone logo
Hone
Problems

Mastering Angular Router Links: Navigating Your Application

In modern single-page applications (SPAs), efficient navigation is crucial for a seamless user experience. Angular's router module provides powerful tools to manage this, with router links being a fundamental building block. This challenge will test your understanding of creating and configuring router links within an Angular application.

Problem Description

Your task is to implement navigation between different components in an Angular application using router links. You will be provided with a basic Angular project structure. You need to:

  1. Create two distinct components: Let's call them HomePageComponent and AboutPageComponent.
  2. Configure the Angular router: Set up routes for both components.
  3. Implement navigation: Use the routerLink directive in your AppComponent's template to create clickable links that navigate to the HomePageComponent and AboutPageComponent.
  4. Display the routed components: Ensure that the router-outlet in AppComponent correctly renders the active component.

Key Requirements:

  • The routerLink directive should be used for navigation.
  • Links should navigate to their respective component routes.
  • The application should successfully display the content of the selected component.

Expected Behavior:

When the AppComponent is loaded, you should see two links (e.g., "Home" and "About"). Clicking the "Home" link should display the content of HomePageComponent. Clicking the "About" link should display the content of AboutPageComponent.

Edge Cases to Consider:

  • Active Link Styling: While not strictly required for this challenge, consider how you might visually indicate which link is currently active. (This can be a good hint for further exploration).

Examples

Example 1: Basic Navigation

Let's assume you have HomePageComponent displaying "Welcome to the Home Page!" and AboutPageComponent displaying "Learn more about us.".

Input: (Conceptual - refers to the state of the application and user interaction)

  • User navigates to the root URL (/).
  • The AppComponent template displays "Home" and "About" links.

Output:

  • The AppComponent's template renders the "Home" and "About" links.
  • The router-outlet displays the content of HomePageComponent ("Welcome to the Home Page!").
  • When the "About" link is clicked, the router-outlet updates to display the content of AboutPageComponent ("Learn more about us.").

Explanation:

The routerLink directive is configured to point to the defined routes (/ for home, /about for about). The router-outlet acts as a placeholder where Angular injects the component associated with the current route.

Constraints

  • You must use the Angular CLI for project setup and component generation.
  • The solution must be implemented using TypeScript.
  • The primary focus is on correctly using the routerLink directive and basic router configuration.

Notes

  • Start by generating the necessary components using ng generate component <component-name>.
  • Remember to import RouterModule and configure your routes in app-routing.module.ts (or app.module.ts if you're not using lazy loading, though app-routing.module.ts is standard).
  • The routerLink directive accepts an array for defining route paths, especially when dealing with route parameters (though not required in this basic challenge). For simple paths, it can be a string or an array with a single string.
  • Consider the [routerLink] syntax for binding to dynamic values, although a static string or array will suffice for this problem.
Loading editor...
typescript