Skip to content

Confusing Information on SharedModule and CoreModule #855

Open
@iamjoyce

Description

@iamjoyce

Documentation for Angular

Questions

  1. Is it recommended to import CommonModule and FormsModule in SharedModule or CoreModule?

In the documentation, both are OK.

Style 04-10 (Shared feature module)
Do import all modules required by the assets in the SharedModule; for example, CommonModule and FormsModule.

Style 04-11 (Core feature module)
Do import all modules required by the assets in the CoreModule (e.g. CommonModule and FormsModule).

--

  1. Are you suggesting to import CoreModule into root AppModule, so that when a lazy loaded module imports the CodeModule, it will get the intended app-wide singleton and not a new instance?

In the documentation, it suggests to import CoreModule into the root AppModule, but yet the subsequent lines mentioned that lazy importing CoreModule will return a new instance.

Style 04-11 (Core feature module)
Why? CoreModule will contain singleton services. When a lazy loaded module imports these, it will get a new instance and not the intended app-wide singleton.

--

  1. Are services supposed to be in CoreModule? If not, what's the recommendation?

Based on the documentation, I thought services are supposed to be in CoreModule, but the last sentence of style 04-11 says otherwise.

Style 04-11 (Core feature module)
Why? You want the entire app to use the one, singleton instance. You don't want each module to have its own separate instance of singleton services. Yet there is a real danger of that happening accidentally if the CoreModule provides a service.

References

This stackoverflow question/answer explains it pretty well, but it seemed to contradict (?) the styleguide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions