2

I was trying to refactor my angular source into multiple feature modules. Earlier the source had only one module and all the components were declared inside that single module. For refactoring, I did following steps:

  1. Created a feature module

  2. Moved a component from app module to feature module. Added the component in feature module in declarations list and exports list.

  3. Removed the references of the moved component from app module

  4. Imported feature module into app module.

The source code compiles, but the site does not load. On the console, I could see it crashes because the component in feature module, on its html has used a component from app module.

error on console

I thought there is no extra step required to use a component from app module in a feature module. Am I missing something?

Added: Below HTML is from the moved component(component that I moved from app module to feature module), which is using app-organization-user-search component declared in app module.

<div class="col-md-12 full-width py-2">
        <app-organization-user-search (addClicked)="addParticipant($event)" (cancelClicked)="addingParticipants = false"></app-organization-user-search>

Added: To all those who are searching answer to the specific problem, the solution is in the comments of the accepted answer.

7
  • Could you give us some example code from your module? Commented Mar 25, 2019 at 12:46
  • So newly moved component has the dependency in the app module? is that correct? Commented Mar 25, 2019 at 12:46
  • @Sudhakar, yes the moved component has a dependency on a component declared in app module Commented Mar 25, 2019 at 12:49
  • @DipenduPaul, Which is your root module? AppModule or future module? Are you importing the future module in App module? Commented Mar 25, 2019 at 12:52
  • can you restart cli ? and make sure name is correct for component and its selector Commented Mar 25, 2019 at 12:52

1 Answer 1

1

For splitting one module into multiple ones, you should mind to export and import all modules and components correctly:

app.module.ts *the „root“ module

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    // angular
    BrowserModule,
    HttpClientModule,
    // splitted modules
    DiversityMattersModule,
    PreventingAirPolutionModule,
    PeacfullyRevolutionModule,
    // ...
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}

diversity-matters.module.ts Example module from list above

@NgModule({
  imports: [
    // ...
  ],
  declarations: [
    AwesomeComponent,
    // ...
  ],
  exports: [
    AwesomeComponent,
    // ...
  ],
})

Every component that depends on AppComponent is now allowed to import AwesomeComponent.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Michael. I have imported the feature module into app module as suggested by you. I understood that a component of app module can use exported component of featured module. But can a component from feature module use a component from app module?
@DipenduPaul, No It can't use the component from app module. If you want then you can create a common module and import the common module to future module and app module.
Ok, that's an eye opener for me. I'll try your solution and mark it as answer. Thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.