📖 Scenario: You are building an Angular app with multiple pages. You want to reuse a button component across these pages without rewriting it each time.
🎯 Goal: Create a shared Angular module that contains a reusable button component. Then import this shared module into another component to use the button.
📋 What You'll Learn
Create a standalone button component with a template showing a button with text 'Click me'
Create a shared Angular module named SharedModule that declares and exports the button component
Import SharedModule into a new standalone component named HomeComponent
Use the button component selector inside HomeComponent's template
💡 Why This Matters
🌍 Real World
Shared modules help organize reusable UI parts like buttons, cards, or forms in large Angular apps, making code easier to maintain and reuse.
💼 Career
Understanding shared modules and reusable components is essential for Angular developers working on scalable, maintainable enterprise applications.
Progress0 / 4 steps
1
Create a standalone button component
Create a standalone Angular component named ButtonComponent with selector app-button. Its template should contain a <button> element with the text Click me. Use the @Component decorator with standalone: true.
Angular
Hint
Use @Component with standalone: true and define the selector and template exactly as instructed.
2
Create a shared module exporting the button component
Create an Angular module named SharedModule using @NgModule. It should declare and export ButtonComponent. Import ButtonComponent in this module file. The module should NOT be standalone.
Angular
Hint
Use @NgModule with declarations and exports arrays including ButtonComponent.
3
Create a standalone HomeComponent importing SharedModule
Create a standalone Angular component named HomeComponent with selector app-home. Import SharedModule in its imports array. The template should contain the <app-button> element to use the button component.
Angular
Hint
Use @Component with standalone: true, import SharedModule, and include <app-button> in the template.
4
Use HomeComponent in your app
Add <app-home> to your main app component template or bootstrap HomeComponent in your Angular app to display the reusable button.
Angular
Hint
Use bootstrapApplication(HomeComponent) to start the app with HomeComponent showing the reusable button.
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
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.
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.
Final Answer:
To group reusable components, directives, and pipes for easy sharing -> Option C
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
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.
Step 2: Check other arrays usage
Imports bring in other modules, providers register services, bootstrap is for root components only.
Final Answer:
Add the component to both declarations and exports arrays in @NgModule -> Option A
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
Step 1: Check module imports and exports
SharedButtonComponent is declared and exported in SharedModule, which is imported by FeatureModule.
Step 2: Confirm component usage in FeatureComponent
Since FeatureModule imports SharedModule, FeatureComponent can use SharedButtonComponent selector without error.
Final Answer:
The button renders correctly with text 'Click' -> Option A
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
Step 1: Check SharedModule exports
SharedCardComponent is declared but not exported, so other modules can't use it.
Step 2: Understand Angular module sharing rules
To use a component from another module, it must be exported by that module.
Final Answer:
SharedCardComponent is not exported from SharedModule -> Option D
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?