JavaScript

Angular 12 Component declared by more than one module

Share your learning

Few months ago, I encountered an error called “component declared by more than one NgModule”. 

Today, I am going to explain and solve this issue. Actually, as we know that typescript is a standardized version of vanilla JavaScript. We need to follow typescript standards while developing so that we could minimize the run time bugs and errors.

Here the concept of Shared Module comes to the picture. We can share components, directive and pipes via a shared ngModule.

Let’s get our hands dirty with some code.

ng generate module form –routing
ng generate module show –routing
ng generate component form
ng generate component show
ng generate component notification
ng generate component header

Now if we just declare the notification and header component in the both form and show modules. It will throw an error :

“The component ___ declared by more than one NgModule”

To solve this issue

We can use the shareModule concept, it says that if you need to use some components, pipes or directives in more than one NgModule then you can create an independent module which is called share module and then declare these components, pipes and directives there. And don’t forget to export these components too, just add these components to the export array of NgModule

After this you can import this module to another module where you want to reuse these components.

FormModule

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormComponent } from './form.component';
import { FormRoutingModule } from './form-routing.module';
import { SharedModule } from '../shared.module';
import { ReactiveFormsModule } from '@angular/forms';
 
@NgModule({
 declarations: [
   FormComponent
 ],
 imports: [
   CommonModule,
   FormRoutingModule,
   ReactiveFormsModule,
   SharedModule
 ]
})
export class FormModule { }

ShowModule

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ShowComponent } from './show.component';
import { ShowRoutingModule } from './show-routing.module';
import { SharedModule } from '../shared.module';
 
 
 
@NgModule({
 declarations: [
   ShowComponent
 ],
 imports: [
   CommonModule,
   ShowRoutingModule,
   SharedModule
 ]
})
export class ShowModule { }

So, let’s create a share module

ng generate module shared

It will generate the sharedModule class with @NgModule decorator which allows us to declare components, import modules and also export the components for reuse purpose.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from './header/header.component';
import { NotificationComponent } from './notification/notification.component';
 
 
 
@NgModule({
 declarations: [
   HeaderComponent,
   NotificationComponent
 ],
 imports: [
   CommonModule
 ],
 exports: [
   HeaderComponent,
   NotificationComponent
 ]
})
export class SharedModule { }

Hope it will help you, if you have any query or suggestions, plz feel free to comment below.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago