Use component from another module -Angular
Last Updated :
16 Apr, 2024
Angular's modular architecture is one of its core strengths, allowing it to break down large applications into smaller, manageable pieces. A key aspect of modularity in Angular is the ability to share components across different modules. You can promote code reuse and maintainability by exporting and importing components between modules. In this article, we'll see how to use Angular's modularity by sharing components across modules.
Module Export and Import
In Angular, modules encapsulate a cohesive set of components, directives, pipes, and services. Modules can be imported into other modules to make their contents available for use. When sharing components across modules, we use the concepts of module export and import:
- Exporting Components: Components that need to be shared across modules are exported from their defining module using the
exports
property of the @NgModule
decorator. - Importing Modules: Modules containing the exported components are imported into other modules where the components are needed using the
imports
property of the @NgModule
decorator.
Approach 1 : Exporting the Component from its Module
Export the component
In the module , wherever the component is defined , it needs to be declared in the declarations array of the `@NgModule` decorator. We also need to export the required component in the `exports` array , so that we can use the component in the other module.
Import the Module
In the module, where we are trying to use the component, we need to import the module that contains the component in the `imports` array.
Use the component
Now we can directly use the component in the templates of components that belong to the importing module.
Example:
Let us take an example to understand the above approach.
Step 1: Install the Angular CLI using the following command
npm install -g @angular/cli
Step 2: Create a new Angular Project.
ng new new-project
cd new-project
Step 3: To start the application run the following command.
ng serve
Step 4: Let us create a new module
cd src
ng g module sample
So here, we are creating a new module named sample .
Step 5 : Let us create a new component inside the module
ng g c loader
The above command creates a new component called loader inside the sample module.

Step 6: Usage of component
HTML
<!-- app.component.html -->
<div>
<h3>Usage of Component from other module:</h3>
<app-loader></app-loader>
</div>
HTML
<!-- loader.component.html -->
<p>loader component from other module works!</p>
JavaScript
//App.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { SampleModule } from 'src/sample/sample.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
SampleModule,
BrowserAnimationsModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
JavaScript
//sample.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoaderComponent } from './loader/loader.component';
@NgModule({
declarations: [LoaderComponent],
imports: [CommonModule],
exports: [LoaderComponent],
})
export class SampleModule { }
Output:

Approach 2 : Moving the Component to the Shared Module
Create the Shared Module
In this approach we will be creating a new module called as Shared module which contains of commonly used and shared component, directives , services, pipes etc, which are used in the entire application multiple times
Export the Component
Whatever component we want to use in other module, we need to export that component by declaring them in exports array of the SharedModule .
Import the Shared Module
In the Module where we want to use the component, we can import the SharedModule. So that we can have access to the components that are exported in the SharedModule
Use the component in the template
Now we can directly use the component in the templates of components that belong to the importing module.
Example:
Let us take an example to understand the above approach.
We have already learned how to create an Angular application in the above example, now let us look how to create a shared module and use the component from shared module in other component.
Step 1:Let us create a new module named as shared
cd src
ng g module shared
So here, we are creating a new module named shared .
Step 2: In the shared module, let us create a component , which is used multiple times across the application
ng g c loader
The above command creates a new component named loader in the shared module.
Step 3 : Now we need to add the component in the exports array of @NgModule decorator.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoaderComponent } from './loader/loader.component';
@NgModule({
declarations: [LoaderComponent],
imports: [CommonModule],
exports: [LoaderComponent],
})
export class SharedModule {}
Step 4: Usage of component
HTML
<!-- app.component.html -->
<div>
<h3>Usage of Component from other module:</h3>
<app-loader></app-loader>
</div>
HTML
<!-- loader.component.html -->
<p>loader component from other module works!</p>
JavaScript
//AppModule.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { SharedModule } from 'src/shared/shared.module';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
BrowserAnimationsModule,
SharedModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
Output:

In this approach we are creating a shared module, which is the most recommended approach , since it contains all the shared components, directives , services etc, and we can directly import SharedModule in other Modules, instead of importing individual modules.
Benefits of Sharing Components Across Modules
- Code Reusability: By sharing components across modules, you can avoid duplicating code and promote code reusability, leading to cleaner and more maintainable codebases.
- Modular Architecture: Sharing components across modules helps maintain a modular architecture, where functionality is organized into cohesive modules with clear boundaries and responsibilities.
- Enhanced Collaboration: Modular codebases with shared components make it easier for multiple developers to collaborate on different parts of the application.
- Performance Optimization: Angular's dependency injection system ensures that shared components are properly managed and instantiated, optimizing performance and memory usage.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read