Bruno Miguel
Bruno Miguel

Reputation: 1115

Understanding angular module imports

Im working on a legacy library which uses in some of its components FormsModules functionalities, so FormsModule is imported at the level of LibraryModule like so:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { FormsModule } from '@angular/forms';

@NgModule({
    declarations: [
        LabelButtonPrimaryComponent,
        TextAreaComponent,
        TextInputComponent,
    ],
    imports: [
        CommonModule,
        FormsModule
    ]})
 export class IronFrameworkModule { }

When i bluid/pack the library, and then install it in some other angular application i still need to import FormsModule in the Application AppModule in order for the components in the IronLibrary to work.

Why is that?

Upvotes: 0

Views: 117

Answers (1)

Ethan Vu
Ethan Vu

Reputation: 2987

A Module in order to be seen and used by others Module, it need to be import in the @NgModule class decorator, and it only visible to that module, so in your case the FormModule only visible to IronFrameworkModule, but not be seen by the AppModule.You then have two option:

  1. Like you did : import the FormModule to your AppModule so it is now seen by AppModule

  2. Export the FormModule from your IronFrameworkModule so whichever Module import IronFrameworkModule, the FormModulewill also be visible to them for use

Alternatively, you should create a Shared Module contain all the Module, Components, Directive ... which would be reused. Then you only need to import that module to others, it centralized the code, so it more easy to maintain.

Upvotes: 1

Related Questions