7

Well, I'm structuring my project following the infrastructure of:

  • Feature module.
  • Core module.
  • Shared module.

But there's something that I still don't have clear enough.

As far as I know, the HttpClientModule should be in the CoreModule because well,... it provides the HttpClient service to make HTTP requests to a server.

Now, the imports array allows an Angular module to use functionalities provided in other modules, and the exports array allows an Angular module to expose some of its functionalities.

I have this in my CoreModule:

@NgModule({
    imports: [
        BrowserAnimationsModule,
        HttpClientModule,
        RouterModule.forRoot(routes, {
            enableTracing: true
        })
    ],
    exports: [
        RouterModule
    ]
})
export class CoreModule {
}

Now, since my CoreModule is imported in my AppModule, shouldn't the HttpClientModule and BrowserAnimationsModule be exported as well? Just as the RouterModule.

I'm seeing the CoreModule and SharedModule like some sort of bridge.

The SharedModule makes more sense to me:

@NgModule({
    imports: [
        MatButtonModule
    ],
    exports: [
        MatButtonModule
    ]
})
export class SharedModule {
}

The SharedModule imports the MatButtonModule and then export it so that other modules can make use of it.

Shouldn't the CoreModule be the same way? Because the App runs fine; however, I'm in dev-mode.

Hope I was clear enough and someone can help me to brush out this doubt.

Alex Peters
  • 2,601
  • 1
  • 22
  • 29
RottenCheese
  • 869
  • 2
  • 12
  • 18

2 Answers2

6

No, the exports array of an NgModule shouldn't include modules without components, directives, or pipes (like HttpClientModule).

The purpose of exporting something from an NgModule is to give other modules (or things within those other modules) access to this module's components, directives, and pipesβ€”and also sometimes to give those other modules access to (i.e. re-export) components, directives, and pipes from some other module as a convenience.

Your CoreModule lists RouterModule as an export so that anything importing your CoreModule can use the routerLink attribute directive (among other things). That is, RouterModule exports components, directives, and/or pipes.

As the HttpClientModule doesn't declare any components, directives, or pipes, there is no value in listing it as an NgModule export.

These Angular guide NgModule FAQs may also be useful:

Alex Peters
  • 2,601
  • 1
  • 22
  • 29
0

I personally think that BrowserAnimationsModule could and should be imported in the AppModule itself.

You do not need to export HttpClientModule if all your services that uses HttpClient is in CoreModule itself.

Even if you do use HttpClient in other modules, you could just import HttpClientModule in that particular module, or do it in AppModule (except for lazy loaded modules).

Joshua Chan
  • 1,797
  • 8
  • 16
  • Possibly someone took umbrage with your second paragraph, which suggests that there might be some other case where exporting a provider-only NgModule is acceptable (there is none). – Alex Peters Sep 23 '18 at 13:33
  • "The CoreModule could also be used to export any third party module that is required in the AppModule. The idea is to keep AppModule as lean as possible" https://blog.chai-jay.com/angular-core-vs-shared-modules/ – LacOniC Nov 18 '19 at 11:26