Selected Reading

Angular - Sharing Modules



This chapter will discuss the Sharing Module in Angular, including how to create and use it in an Angular project, along with its advantages.

Sharing Module in Angular

In Angular, the Sharing Module is a custom module created by the developer based on the application's requirements. The developer can easily access its components, directives, pipes, etc.

Initially, a module is not considered a shared module until it is properly exported and imported into other modules where its components and dependencies are required.

Why use a Sharing Module?

The sharing module provides developers with reusable code that can be used across the entire Angular project, rather than recreating them in each module.

For example, if we need a header and footer for our project, instead of creating them in each component, we can create a shared module and place these components within that module. Then, we can use them across the entire application.

Creating Sharing Module

Creating a sharing module allows you to organize and simplify your code, by reducing the redundancy of repeating the same things within the same project.

Use the following command to create a module in your Angular application, and this module will be used as a shared module in the application. We import and export this module within the other module where it's component are needed.

Step 1: Open the project

Open your application in any code editor (such as VS code).

Step 2: Navigate to the Application Directory

Open the code editor terminal and navigate to your application directory as follows:

cd your_application_directory

e.g.
cd myapp

Step 3: Create a new module (i.e., consider it a sharing module) named Shared as follows:

ng generate module shared

Once the above command is executed successfully, you should be able to see the shared folder. Within this folder, there will be a file named shared.module.ts with a default code:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

How to Use Sharing Module?

To use the Sharing Module in our Angular application, the first step is to import it into our root module, which is named AppModule in our application.

Step 1: Open the app.module.ts file and import the shared module as follows:

import { NgModule } from '@angular/core';
import { BrowserModule, provideClientHydration } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SharedModule } from './shared/shared.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule, 
    SharedModule
  ],
  providers: [
    provideClientHydration()
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

We have already created a sharing module in our Angular application and imported it into the AppModule (i.e., root module). Now, let's create two components within the Shared Module to use throughout the entire application.

Step 2: Create a Header Component

Create a new component, Header within the shared module as follows:

ng generate component shared/header

Step 3: Open the header.component.ts file and place the below code:

<h2>This his Header</h2>
<p>A component from Shared Module</p>

Step 4: Create another component, Footer.

Create one more component, Footer within the shared module as follows:

ng generate component shared/footer

Step 5: Open the footer.component.ts file and place the below code:

<h2>This his Footer</h2>
<p>A component from Shared Module</p>

Step 6: Update the Shared Module

To use the "components" of the Shared Module outside of this module, you need to export the Header and Footer components in the shared.module.ts file as follows:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';

@NgModule({
  declarations: [
    HeaderComponent,
    FooterComponent
  ],
  imports: [
    CommonModule
  ],
  exports: [HeaderComponent, FooterComponent]
})
export class SharedModule { }

Step 7: Update AppComponent

Open the app.component.html and place the code below:

<app-header></app-header>
<hr>
<p>This is the loaded area</p>
<hr>
<app-footer></app-footer>

Step 8: Run the application using the following command:

ng serve

Step 9: Open your friendly browser (chrome) and navigate to localhost:4200:

Advantages of Sharing Module

The following is a list of the advantages of the Sharing Modules:

  • It increases application performance by using "reusable" components, directives, etc.
  • Organizes the application, streamlining the code.
  • Increases application modularity.
  • Decreases redundancy.

Conclusion

In conclusion, observing the above output should provide you with a complete understanding of how the Sharing Modules operates. By sharing this module, we can use its "components", "directives", and "pipes" across the entire application rather than creating them multiple times within the same application.

Advertisements