Angular - Should HttpClientModule be in the Exports array in the CoreModule?
Asked Answered
G

2

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.

Gee answered 9/5, 2018 at 20:38 Comment(0)
D
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:

Detradetract answered 23/9, 2018 at 13:30 Comment(1)
Thanks for the linksAvionics
F
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).

Foulness answered 9/5, 2018 at 20:43 Comment(2)
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).Detradetract
"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" blog.chai-jay.com/angular-core-vs-shared-modulesFogbow

© 2022 - 2024 — McMap. All rights reserved.