Hone logo
Hone
Problems

Angular Shared Module Creation Challenge

Your task is to create a reusable "Shared Module" in an Angular application. This module will serve as a central place to export common Angular components, directives, pipes, and services that can be easily imported and used across different feature modules within your application. This promotes code organization, reusability, and maintainability.

Problem Description

You are tasked with developing a well-structured Angular application that leverages a shared module for common functionalities. You need to create this SharedModule and populate it with a few example items. The goal is to demonstrate how to correctly define, import, and export these reusable pieces of code.

What needs to be achieved:

  1. Create a new Angular module named SharedModule.
  2. Define and include a simple Angular component within SharedModule. This component should be generic enough to be useful in various contexts.
  3. Define and include a simple Angular pipe within SharedModule. This pipe should perform a basic transformation.
  4. Export the component and the pipe from SharedModule. This makes them available for import by other modules.
  5. Create a separate Angular feature module (e.g., UserModule or ProductModule) that imports SharedModule.
  6. Use the exported component and pipe from SharedModule within a component belonging to the feature module.

Key Requirements:

  • The SharedModule should be declared in its own TypeScript file (e.g., shared.module.ts).
  • The component and pipe must be declared within SharedModule.
  • Both the component and the pipe must be listed in the exports array of SharedModule's @NgModule decorator.
  • The feature module must import SharedModule in its imports array.
  • The feature module's component should utilize the exported component and pipe.
  • The application should compile and run without errors, displaying the output of the shared component and pipe.

Expected Behavior:

When the application loads, a component within your feature module should correctly render the instance of the shared component and apply the shared pipe's transformation to some data.

Edge Cases to Consider:

  • Circular Dependencies: While not explicitly tested with complex scenarios in this challenge, be mindful of how you structure imports to avoid circular dependencies between modules.
  • Service Reusability: Consider how services would be handled if they were to be shared. For this challenge, we will focus on components and pipes.

Examples

Example 1: Basic Shared Component and Pipe Usage

Let's imagine a scenario where you have a HeaderComponent and a FormatDatePipe.

  • SharedModule structure:

    • SharedModule declares HeaderComponent and FormatDatePipe.
    • SharedModule exports HeaderComponent and FormatDatePipe.
  • FeatureModule structure:

    • FeatureModule imports SharedModule.
    • FeatureModule has a DashboardComponent.
    • DashboardComponent uses <app-header></app-header> and displays a date formatted by {{ currentDate | formatDate }}.

Input: (Conceptual - refers to the Angular project setup) A standard Angular project created with the Angular CLI.

Output: (Conceptual - refers to the rendered UI) The DashboardComponent renders a header provided by HeaderComponent and displays a date that has been formatted by FormatDatePipe.

Explanation: The FeatureModule imports SharedModule, making HeaderComponent and FormatDatePipe available for use. DashboardComponent then instantiates HeaderComponent and applies FormatDatePipe to a date variable.

Constraints

  • You must use TypeScript for all Angular code.
  • The Angular CLI must be used for project setup and module generation.
  • The solution should be a standalone, runnable Angular project (or a clear set of files and instructions to integrate into an existing one).
  • No third-party libraries beyond Angular's core framework should be used for the shared module's functionality.

Notes

  • Remember that modules declared in SharedModule are not automatically available to other modules that import SharedModule. They must be explicitly listed in the exports array.
  • Services intended for sharing across multiple feature modules are typically provided in the root module (e.g., AppModule) or a dedicated CoreModule that is imported only once in the application's lifecycle. For this challenge, focus on components and pipes.
  • Think about the naming conventions for your shared components and pipes to ensure they are easily identifiable.
Loading editor...
typescript