Bird
Raised Fist0
Angularframework~10 mins

Shared modules for reusable components in Angular - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the shared module in the app module.

Angular
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { [1] } from './shared/shared.module';

@NgModule({
  imports: [BrowserModule, SharedModule],
  bootstrap: [AppComponent]
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
ASharedModule
BCommonModule
CHttpClientModule
DFormsModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Angular built-in modules instead of the shared module.
Forgetting to import the shared module.
2fill in blank
medium

Complete the code to export a reusable component from the shared module.

Angular
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReusableComponent } from './reusable/reusable.component';

@NgModule({
  declarations: [ReusableComponent],
  imports: [CommonModule],
  exports: [[1]]
})
export class SharedModule {}
Drag options to blanks, or click blank then click option'
ABrowserModule
BCommonModule
CAppComponent
DReusableComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Exporting modules instead of components.
Not exporting the reusable component.
3fill in blank
hard

Fix the error in the shared module by completing the imports array.

Angular
import { NgModule } from '@angular/core';
import { [1] } from '@angular/common';

@NgModule({
  imports: [CommonModule],
  declarations: [],
  exports: []
})
export class SharedModule {}
Drag options to blanks, or click blank then click option'
ACommonModule
BBrowserModule
CFormsModule
DHttpClientModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BrowserModule in shared modules (should only be in root module).
Forgetting to import CommonModule.
4fill in blank
hard

Fill both blanks to create a shared module that declares and exports a component.

Angular
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { [1] } from './button/button.component';

@NgModule({
  declarations: [[2]],
  imports: [CommonModule],
  exports: [ButtonComponent]
})
export class SharedModule {}
Drag options to blanks, or click blank then click option'
AButtonComponent
BAppComponent
CCommonModule
DFormsModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong component names in import or declarations.
Not declaring the component before exporting.
5fill in blank
hard

Fill all three blanks to create a shared module that imports CommonModule, declares, and exports a component.

Angular
import { NgModule } from '@angular/core';
import { [1] } from '@angular/common';
import { [2] } from './card/card.component';

@NgModule({
  declarations: [[3]],
  imports: [CommonModule],
  exports: [CardComponent]
})
export class SharedModule {}
Drag options to blanks, or click blank then click option'
ACommonModule
BCardComponent
CButtonComponent
DFormsModule
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing component names in import and declarations.
Forgetting to import CommonModule.

Practice

(1/5)
1. What is the main purpose of a shared module in Angular?
easy
A. To store environment configuration settings
B. To define the root component of the application
C. To group reusable components, directives, and pipes for easy sharing
D. To handle routing between different pages

Solution

  1. Step 1: Understand the role of shared modules

    Shared modules are designed to hold components, directives, and pipes that multiple parts of the app can use.
  2. Step 2: Compare with other module purposes

    Root modules handle app startup, routing modules manage navigation, and environment files store settings, not reusable UI parts.
  3. Final Answer:

    To group reusable components, directives, and pipes for easy sharing -> Option C
  4. Quick Check:

    Shared module = reusable parts [OK]
Hint: Shared modules bundle reusable UI parts for easy import [OK]
Common Mistakes:
  • Confusing shared modules with root or routing modules
  • Thinking shared modules store app settings
  • Assuming shared modules handle navigation
2. Which of the following is the correct way to export a component from a shared module?
easy
A. Add the component to both declarations and exports arrays in @NgModule
B. Add the component only to the imports array
C. Add the component only to the providers array
D. Add the component only to the bootstrap array

Solution

  1. Step 1: Understand Angular module metadata

    Components must be declared in declarations to be recognized, and exported in exports to be usable outside the module.
  2. Step 2: Check other arrays usage

    Imports bring in other modules, providers register services, bootstrap is for root components only.
  3. Final Answer:

    Add the component to both declarations and exports arrays in @NgModule -> Option A
  4. Quick Check:

    Declare + export = share component [OK]
Hint: Declare and export components to share them outside module [OK]
Common Mistakes:
  • Putting components only in imports or providers
  • Forgetting to export after declaring
  • Using bootstrap array in shared modules
3. Given this shared module code, what will be the output when FeatureComponent uses SharedButtonComponent?
@NgModule({
  declarations: [SharedButtonComponent],
  exports: [SharedButtonComponent]
})
export class SharedModule {}
@NgModule({
  imports: [SharedModule],
  declarations: [FeatureComponent]
})
export class FeatureModule {}
If FeatureComponent template contains <app-shared-button>Click</app-shared-button>, what happens?
medium
A. The button renders correctly with text 'Click'
B. Error: 'app-shared-button' is not a known element
C. The button renders but without text
D. The app crashes at runtime

Solution

  1. Step 1: Check module imports and exports

    SharedButtonComponent is declared and exported in SharedModule, which is imported by FeatureModule.
  2. Step 2: Confirm component usage in FeatureComponent

    Since FeatureModule imports SharedModule, FeatureComponent can use SharedButtonComponent selector without error.
  3. Final Answer:

    The button renders correctly with text 'Click' -> Option A
  4. Quick Check:

    Imported shared module exports = component usable [OK]
Hint: Import shared module to use its exported components [OK]
Common Mistakes:
  • Not importing SharedModule in FeatureModule
  • Forgetting to export component in SharedModule
  • Using component selector incorrectly
4. Identify the error in this shared module setup:
@NgModule({
  declarations: [SharedCardComponent],
  imports: [CommonModule]
})
export class SharedModule {}
@NgModule({
  imports: [SharedModule],
  declarations: [DashboardComponent]
})
export class DashboardModule {}
DashboardComponent template uses <app-shared-card> but Angular throws an error.
medium
A. CommonModule should not be imported in SharedModule
B. DashboardComponent should import SharedCardComponent directly
C. DashboardModule should declare SharedCardComponent again
D. SharedCardComponent is not exported from SharedModule

Solution

  1. Step 1: Check SharedModule exports

    SharedCardComponent is declared but not exported, so other modules can't use it.
  2. Step 2: Understand Angular module sharing rules

    To use a component from another module, it must be exported by that module.
  3. Final Answer:

    SharedCardComponent is not exported from SharedModule -> Option D
  4. Quick Check:

    Declare + export needed to share component [OK]
Hint: Always export components to share them outside module [OK]
Common Mistakes:
  • Forgetting to export declared components
  • Trying to redeclare components in feature modules
  • Misunderstanding imports vs exports
5. You want to create a shared module that provides a reusable button component and a custom pipe. Which setup correctly allows any feature module to use both without redeclaring them?
// SharedModule
@NgModule({
  declarations: [SharedButtonComponent, CustomPipe],
  exports: [SharedButtonComponent, CustomPipe],
  imports: [CommonModule]
})
export class SharedModule {}
// FeatureModule
@NgModule({
  imports: [SharedModule],
  declarations: [FeatureComponent]
})
export class FeatureModule {}
What must you do in FeatureComponent template to use both?
hard
A. Import SharedButtonComponent and CustomPipe directly in FeatureComponent
B. Use <app-shared-button> and apply | customPipe in template expressions
C. Declare SharedButtonComponent and CustomPipe again in FeatureModule
D. Use <shared-button> and | CustomPipe with capital letters

Solution

  1. Step 1: Confirm shared module exports

    SharedButtonComponent and CustomPipe are declared and exported in SharedModule, which FeatureModule imports.
  2. Step 2: Use correct selectors and pipe names

    FeatureComponent can use selector and apply | customPipe (pipe names are case-sensitive and lowercase by convention).
  3. Final Answer:

    Use <app-shared-button> and apply | customPipe in template expressions -> Option B
  4. Quick Check:

    Import shared module + use selectors and pipe names correctly [OK]
Hint: Use exported selectors and pipe names exactly as declared [OK]
Common Mistakes:
  • Redeclaring shared components or pipes in feature modules
  • Using wrong selector or pipe name casing
  • Trying to import components or pipes directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard