What is the role of Angular core module?

僤鯓⒐⒋嵵緔 提交于 2021-01-20 19:52:20

问题


I read an article about organizing folders in Angular and the author uses one called core, in which he creates the core module and insert only services which will be called once.

Currently, I'm working on a project whereupon I adopted the structure recommended by the article. Inside my core folder/module I created a module called api:

\app
  \core
    \api
    ...
  \shared
  ...

My question is: as the api is a module, I was wondering why not remove it from core and put in app folder?

\app
  \api
  \core
  \shared
  ...

Angular itself has core module too (where we import ngModule, Injectable...) and I'd like to know what is its role.


回答1:


Your CoreModule contains code that will be used to instantiate your app and load some core functionality.

To get more idea on this read Core Module




回答2:


Generally Core module is used in order to keep all API services and other common services (like NotificationsService, ConfirmationService, AuthorizationService etc) that you have in your application in the providers[... ] section. Then, the app.module will use your core.module in the imports section.




回答3:


This concept is a bit confusing, and I grasp it by reading many posts. To make understanding core module, I like share my idea related to Core module.

The main reason for generating CoreModule is making AppModule a bit leaner.

For example, imagine, some services like AnalyticService are provided in AppModule, and these services should be instantiated once in whole of your app. To make AppModule cleaner, you could generated CoreModuleand put them into this module.



来源:https://stackoverflow.com/questions/49084853/what-is-the-role-of-angular-core-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!