Is the custom directive malfunctioning within the Angular module?

Within my application, there are two modules: IdentityModule and ServiceModule. These modules are loaded using lazy loading technology.

I've recently developed a custom directive called IconSpacerDirective, which is bound to app.module.ts. This directive functions perfectly within my app.component.ts.

However, I am encountering an issue when trying to use the IconSpacerDirective within the IdentityModule. The error message reads:

ERROR Error: Uncaught (in promise): Error: Type IconSpacerDirective is part of the declarations of 2 modules: AppModule and IdentityRegistryModule! Please consider moving IconSpacerDirective to a higher module that imports AppModule and IdentityRegistryModule. You can also create a new NgModule that exports and includes IconSpacerDirective then import that NgModule in AppModule and IdentityRegistryModule.
Error: Type IconSpacerDirective is part of the declarations of 2 modules: AppModule and IdentityRegistryModule! Please consider moving IconSpacerDirective to a higher module that imports AppModule and IdentityRegistryModule. You can also create a new NgModule that exports and includes IconSpacerDirective then import that NgModule in AppModule and IdentityRegistryModule.

Below is the snippet from my identityRegistryModule:

import { IconSpacerDirective } from '../shared/custom-directive/icon-spacer.directive';
@NgModule({
  declarations: [
    IconSpacerDirective
  ],
  imports: [
    IdentityRegistryRoutingModule,
    MaterialModule   

  ],
  providers: [
    IdentityService,
  ],
})
export class IdentityRegistryModule { }

The code from my app.module.ts is as follows:

import { IconSpacerDirective } from './shared/custom-directive/icon-spacer.directive';

@NgModule({
  declarations: [
    AppComponent,
    MainNavComponent,
    SideNavComponent,
    HeaderComponent,
    HomeComponent,
    IconSpacerDirective,

  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    FlexLayoutModule,
    BrowserAnimationsModule,
    LayoutModule,
    MaterialModule,
    OAuthModule.forRoot(),
  ],
  providers: [],
  bootstrap: [AppComponent],
  //exports: [IconSpacerDirective]
})
export class AppModule { }

Is there a way for me to utilize the IconSpacerDirective within my identityRegistryModule?

Answer №1

To make use of the IconSpacerDirective in both the AppModule and the IdentityRegistryModule, it is necessary to create a dedicated module for this directive that can be imported into the required modules.

@NgModule({
  declarations: [IconSpacerDirective],
  exports: [IconSpacerDirective]
})
export class IconSpacerModule { }
@NgModule({
  imports: [IconSpacerModule]
})
export class AppModule { }
@NgModule({
  imports: [IconSpacerModule]
})
export class IdentityRegistryModule { }

Answer №2

If you have already imported the module, there is no need to declare it again. Remove the declaration of IdentityRegistryModule as per the error message.

Make sure to export the IconSpacerDirective in your app.module:

@NgModule({
  ...
  exports: [IconSpacerDirective] // Allow use when imported
})
export class AppModule { }

Also, include the app.module in the identityRegistryModule:

@NgModule({
  imports: [
    IdentityRegistryRoutingModule,
    MaterialModule,
    AppModule  
  ]
  ...
})
export class IdentityRegistryModule { }

Answer №3

If you wish to utilize the directive exclusively within IdentityModule, there is no need to include it in App.module. Simply declare it in Identity module and make use of it there. However, if you intend to use the directive across multiple modules, including components in app.module, then it is recommended to create a separate module for common directives like:

Common Directive Module

@NgModule({
 declarations: [IconSpacerDirective],
 exports:[IconSpacerDirective]
})
export class MyCommonModule{}

You can then export it wherever needed. For example, if you want to use it for components in both app.module and Identity module, follow the logic below:

app.module.ts

@NgModule({
imports: [MyCommonModule]
})

identity.module.ts

import { IconSpacerDirective } from '../shared/custom-directive/icon-spacer.directive';

@NgModule({
  imports: [
    IdentityRegistryRoutingModule,
    MaterialModule,
    **MyCommonModule**
  ],
  providers: [
    IdentityService,
  ],
})
export class IdentityRegistryModule { }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Strange Appearance of Angular Material Slider

I am experiencing some issues with my slider and I'm not exactly sure what's causing them. [] .big-container { display: block; padding-left: 10px; padding-right: 10px; padding-top: 10px; margin-bottom: 10px; } .question { display ...

Separate an HTML tag along with its content at the caret position into several distinct tags

Having some issues with splitting tag contents at caret position. The spliting index and the tag contents after splitting are accessible, however, encountering a problem as described below. <html> <div contenteditable="true"> <s ...

Splitting Angular 4 code using angular-cli

My project is being built using angular-cli (ng build --prod), but I am encountering three issues in my production build: The rendering blocking style-sheet is 74 kb The vendor.bundle.js is extremely large at 1.1 MB The main.bundle.js is also quite large ...

Error in binding dynamic component was encountered

Our Angular 2 application includes the use of the Kendo Combobox component. This particular component is wrapped within a dynamically created component during runtime. The process for creating this component is quite simple: let factory = this.resolver ...

Utilize the super type as a generic parameter in TypeScript for stronger assertions

The Query Within TypeScript, utilizing the keyword extends allows for asserting a generic parameter as a 'subtype' of another type. For example: // class Base {} // class Child extends Base {} // edited: class Base { a = 1 } class Child extends ...

Comparing Passing Props with Extracting Cache in Apollo Client for Next.js applications

Hello there! I am diving into the world of apollo and graphql, specifically interested in SSR/SSG. While I have a basic understanding of what SSR/SSG entails, implementing it with apollo-client is a bit tricky for me. After some extensive searching online ...

Require a parameter in the return function when the generic is not null in the caller within Typescript

In TypeScript version 5.0.2 I am working on a function that returns an array of 3 functions. I want the purchase function to be typed in such a way that it only requires a requirement parameter if the specified product has one (as indicated in the product ...

Establishing a custom variable within a recurring component in a form for the purpose of validation

For my booking form process, I have different sections for adults, pensioners, children, and infants. While they all share the same four inputs, each section also has some unique input fields based on the type of customer. To streamline the duplicate secti ...

The type {properties .....} is incompatible with the type ActionReducer<AdminState, Action> in Angular 12 using NGRX

Implementing NGRX library for redux to organize the state of the application in a structured way: export interface ApplicationState { adminState: AdminState } export interface AdminState { adminProductCategory: ProductCategoryState; adminProdu ...

How to automatically close an Angular 2 material dialog

Using angular2 material's MdDialog, I have implemented a form display feature. Upon form submission, a request is made to the backend. If the request is successful, I need to automatically close the dialog. However, if the backend request fails, the ...

Having difficulty resolving sub-modules using webpack

Currently, I am trying to set up the @microsoft/signalr npm package with webpack by importing the module using import * as signalR from '@microsoft/signalr'. However, I encountered an error message indicating that webpack is unable to resolve the ...

Does Typescript fail to recognize the "delete" operator?

Whenever I utilize the delete operator in Typescript, it appears that the system does not recognize that the property has been eliminated. For instance: interface HasName { name: string; } interface HasNoName { name: never; } function removeName( ...

Ways to limit the combination of general types in Typescript?

Struggling to develop a React form component with generic types. The initialValues parameter determines the generic type for the form. Unable to figure out how to specify the type for each field in Typescript. Check out my CodeSandbox where I've at ...

In React TypeScript, the property types of 'type' are not compatible with each other

I have a unique custom button code block here: export enum ButtonTypes { 'button', 'submit', 'reset', undefined, } type CustomButtonProps = { type: ButtonTypes; }; const CustomButton: React.FC<CustomButtonProp ...

Refreshing/reloading Angular 9 SSR pages

Encountering an issue with Angular and SSR. Running angular 9, everything runs smoothly except when I refresh the page (F5), it first displays the login page before loading the current page. Previously built with angular 8, where this problem did not occur ...

Create a feature in Angular that allows for the dynamic addition of input fields that are connected to a generic

I am dealing with a standard key value array cost: {[ key: string ] : number}; I am exploring ways to configure a dynamic input field <input name="costCat" [(ngModel)] = "???"/> <input name="costVal" [(ngModel)] = & ...

What is the best way to create a case-insensitive sorting key in ag-grid?

While working with grids, I've noticed that the sorting is case-sensitive. Is there a way to change this behavior? Here's a snippet of my code: columnDefs = [ { headerName: 'Id', field: 'id', sort: 'asc', sortabl ...

Troubleshooting TypeScript errors related to ReactNode post-upgrade to Create React App v5

Since upgrading to Create React App version 5, I've been encountering errors like the one below: TS2786: 'OutsideClickHandler' cannot be used as a JSX component. Its instance type 'OutsideClickHandler' is not a valid JSX element. ...

Combining the power of Angular CLI with Squarespace

Has anyone managed to successfully integrate Angular CLI into a Squarespace website? I've been trying to find a solution to this issue but haven't had any luck. While it's possible to add custom scripts or use CDNs on Squarespace, deploying ...

Angular allows cross-origin requests with Access-Control-Allow-Origin

Hi, I am facing an issue with the update function in my app. Whenever I try to update, it shows me the following error message: Failed to load http:// localhost:8080/../update: Response to preflight request doesn't pass access control check: No &apo ...