Hone logo
Hone
Problems

Angular Basic Router Implementation

This challenge focuses on implementing fundamental routing in an Angular application. Routing is crucial for building Single Page Applications (SPAs) by allowing users to navigate between different views without full page reloads, providing a smoother and more responsive user experience. Your task is to set up a simple Angular application with two components and configure routing to switch between them.

Problem Description

You need to create an Angular application with the following structure:

  • AppComponent: This is the root component and will contain the navigation links.
  • HomeComponent: This component will display content for the home page.
  • AboutComponent: This component will display content for the about page.

The application should have two navigation links in the AppComponent: "Home" and "About". Clicking "Home" should navigate to the HomeComponent, and clicking "About" should navigate to the AboutComponent. The URL in the browser should update accordingly (e.g., /home and /about).

Key Requirements:

  • Use Angular's Router module.
  • Define routes for both the HomeComponent and AboutComponent.
  • Implement navigation links in the AppComponent that trigger route changes.
  • Ensure the application renders the correct component based on the current route.

Expected Behavior:

  1. The application starts at the root route (/). Initially, the HomeComponent should be displayed.
  2. Clicking the "Home" link should navigate to the HomeComponent and update the URL to /home.
  3. Clicking the "About" link should navigate to the AboutComponent and update the URL to /about.
  4. Directly navigating to /home in the browser should display the HomeComponent.
  5. Directly navigating to /about in the browser should display the AboutComponent.
  6. Navigating to any other route (e.g., /) should display the HomeComponent.

Edge Cases to Consider:

  • Handling invalid routes (though a full error page isn't required, ensure the application doesn't crash). For simplicity, default to the HomeComponent.
  • Ensure the navigation links are visually distinct and clearly indicate the current route.

Examples

Example 1:

Input: User clicks "About" link from AppComponent.
Output: AboutComponent is displayed, and the browser URL changes to `/about`.
Explanation: The router detects the click event, matches the route `/about`, and renders the AboutComponent.

Example 2:

Input: User types `/home` into the browser's address bar.
Output: HomeComponent is displayed, and the browser URL remains `/home`.
Explanation: The router detects the URL change, matches the route `/home`, and renders the HomeComponent.

Example 3:

Input: User types `/nonexistent-route` into the browser's address bar.
Output: HomeComponent is displayed, and the browser URL remains `/nonexistent-route`.
Explanation: The router doesn't have a defined route for `/nonexistent-route`, so it defaults to the HomeComponent.

Constraints

  • The application should be a functional Angular application.
  • Use Angular version 16 or later.
  • Keep the code concise and readable.
  • Focus on the core routing functionality; advanced features like route parameters are not required for this challenge.
  • The solution should not include any external libraries beyond Angular itself.

Notes

  • You'll need to create an Angular project using the Angular CLI (ng new my-routing-app).
  • Remember to import and configure the RouterModule in your AppModule.
  • Use the <router-outlet> directive in the AppComponent to display the routed components.
  • Consider using <a routerLink="..."> for navigation links.
  • Think about how to define routes using RouterModule.forRoot() or RouterModule.forChild().
Loading editor...
typescript