Discover how Angular's smart sharing system saves you from endless manual wiring and confusion!
Why Declarations, imports, and exports in Angular? - Purpose & Use Cases
Imagine building a big house where every room needs to know about the furniture and tools in other rooms, but you have to carry each item by hand every time you want to use it somewhere else.
Manually managing which parts of your app can use which features is like carrying furniture room to room--it's slow, confusing, and easy to lose things or break stuff.
Angular's declarations, imports, and exports let you organize your app like a smart house with rooms that share furniture automatically, so everything is in the right place and ready to use without extra effort.
const feature = require('./feature');
app.use(feature);import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FeatureComponent } from './feature.component'; @NgModule({ declarations: [FeatureComponent], imports: [CommonModule], exports: [FeatureComponent] }) export class FeatureModule {}
This system makes your app easy to build, understand, and grow by clearly sharing and reusing parts without confusion or mistakes.
Think of a kitchen module that declares a stove and exports it so the dining room module can import and use the stove without rebuilding or copying it.
Declarations tell Angular what components belong to a module.
Imports bring in features from other modules to use inside your module.
Exports share your module's features so other modules can use them easily.
Practice
declarations array inside an NgModule?Solution
Step 1: Understand the role of declarations in NgModule
Thedeclarationsarray is used to tell Angular which components, directives, and pipes belong to this module.Step 2: Differentiate declarations from imports and exports
Imports bring in other modules, exports share parts with other modules, and declarations define what is inside this module.Final Answer:
To list components, directives, and pipes that belong to this module -> Option AQuick Check:
Declarations = Own components/directives/pipes [OK]
- Confusing declarations with imports
- Thinking declarations export components
- Mixing declarations with service providers
CommonModule into an Angular module?Solution
Step 1: Identify where modules are added
Other modules likeCommonModuleare added inside theimportsarray to bring their features in.Step 2: Confirm correct syntax
Usingimports: [CommonModule]is the correct syntax to import a module.Final Answer:
imports: [CommonModule] -> Option BQuick Check:
Modules go in imports [OK]
- Putting modules in declarations instead of imports
- Using exports to import modules
- Adding modules to providers
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './my.component';
@NgModule({
declarations: [MyComponent],
imports: [CommonModule],
exports: []
})
export class MyModule {}Which of the following is true about
MyComponent?Solution
Step 1: Check declarations and exports
MyComponentis declared but not exported inMyModule.Step 2: Understand component visibility
Declared components are only usable inside the module unless exported. SinceMyComponentis not exported, other modules cannot use it.Final Answer:
It can only be used inside templates of components declared in MyModule -> Option CQuick Check:
Declared but not exported = local use only [OK]
- Assuming declarations export components automatically
- Thinking imports export components
- Confusing imports with declarations
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyComponent } from './my.component';
@NgModule({
declarations: [],
imports: [BrowserModule, MyComponent],
exports: [MyComponent]
})
export class AppModule {}Solution
Step 1: Identify where components belong
Components likeMyComponentmust be listed indeclarations, notimports.Step 2: Check imports and exports usage
Modules go inimports, components go indeclarations. Exporting components is allowed.Final Answer:
MyComponentshould be in declarations, not imports -> Option DQuick Check:
Components belong in declarations [OK]
- Putting components inside imports
- Thinking exports can't include components
- Confusing providers with declarations
SharedModule declares and exports a component ButtonComponent. You want to use ButtonComponent inside FeatureModule. Which is the correct way to set this up?Solution
Step 1: Understand exports and imports
SharedModuleexportsButtonComponent, so importingSharedModuleinFeatureModulemakesButtonComponentavailable.Step 2: Avoid redeclaring exported components
Do not redeclareButtonComponentinFeatureModule; just importSharedModule.Final Answer:
Import SharedModule in FeatureModule without declaring ButtonComponent again -> Option AQuick Check:
Import module exporting component, no redeclaration needed [OK]
- Redeclaring exported components in importing module
- Not importing the module that exports the component
- Trying to export the importing module instead
Ready to prove it?
Pass the test and this pattern moves unknown
