在功能模块中导入 HttpClientModule [英] Importing HttpClientModule in feature modules

查看:31
本文介绍了在功能模块中导入 HttpClientModule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpClientModule 上的 docs 说:

在使用 HttpClientModule 之前,您需要导入 AngularHttpClient 模块.大多数应用都在根 AppModule 中执行此操作.

Before you can use the HttpClientModule, you need to import the Angular HttpClientModule. Most apps do so in the root AppModule.

  1. 在这种情况下,我们要将 HttpClientModule 导入到功能模块?
  2. 如果我们将其导入到多个哪些功能模块又被导入根模块?
  3. 当我们在一个应用程序中有几十个模块,其中一些需要 HttpClientModule 并且我们想将 HttpClientModule 导入时,这是一个合适的用例吗?只有那几个?
  1. In which cases we want to import the HttpClientModule into feature modules?
  2. What would happen if we imported it into multiple feature modules which, in turn, are impoted into the root module?
  3. Is this a proper use case of it when we have dozens of modules in an app, a few of which need HttpClientModule, and we would like to import HttpClientModule into those few ones only?

推荐答案

所以,有两种方法可以在你的根模块中加载模块,

So, there are two ways to load modules in your root module,

  1. 将其添加到导入数组中
  2. 延迟加载

现在当你在导入数组中加载一个模块时,该模块提供的所有服务都变成了单例服务,也就是说,只有这些服务的单个实例将在整个应用程序中共享.这些服务的实例是在您的应用程序启动时由根注入器创建的.

Now when you load a module in imports array, all the services provided by that module become singleton services, that is, only single instance of those services will be shared throughout your application. And the instance of those services are created by root injector at the time of bootstrap of your application.

就服务而言,无论是在根模块中加载一个模块还是在多个功能模块中加载,然后将所有这些功能模块加载到根模块中,最终都会得到服务的单个实例.

In terms of services, it does not matter if you load a module in root module or multiple feature modules, and then you load all those feature modules in your root module, you will end up with single instances of the services.

但是,就您的声明而言,即组件、管道、指令.如果您想将 AModule 用于其导出的组件,则必须在要使用这些组件的功能模块中加载 AModule.

But, in terms of your declarations, i.e. Components, Pipes, directives. If you want to use an AModule for the components it exports, you will have to load AModule in the feature module in which you want to use the components.

但是,您可以查看 HttpClientModule 源代码.声明或导出数组中没有任何内容.它只提供服务,所以不管你是在你的功能模块中加载它(同时在根模块的导入数组中加载功能模块),还是在根模块中,你都会以任何一种方式获得它的服务.因此,只需将其加载到根模块中即可.

But, you can look at the HttpClientModule source_code. there is nothing in the declarations or exports array. It only provide services, so it does not matter if you load it in your feature module(while loading the feature module in root module's imports array), or in Root module, you will get it's services either way. So, just load it in the Root Module.

现在当你使用延迟加载来加载一个特性模块时,延迟加载的模块确实获得了根模块中所有其他模块提供的所有服务,但它有自己的注入器,这意味着如果你在延迟加载模块中加载 HttpClientModule已加载到根模块中的加载模块,您最终将获得 HttpClientModule 提供的所有服务的两个实例,而您不希望那样.

Now when you use Lazy loading to load a feature module, the lazily loaded module does get all the services provided by all other modules in the root module, but it has it's own injector, that means if you load HttpClientModule in the lazily loaded module which is already loaded in root module, you will end up with two instances of all the services provided by HttpClientModule, and you don't want that.

您可以进一步阅读有关 forRoot 和 forChild 模式的更多信息来解决此类情况.

You can further read more about forRoot and forChild pattern to tackle such situations.

这篇关于在功能模块中导入 HttpClientModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆