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

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

问题描述

HttpClientModule上的文档说:

在使用HttpClientModule之前,需要导入Angular HttpClientModule.大多数应用程序都是在根AppModule中这样做的.

  1. 在这种情况下,我们要将HttpClientModule导入到 功能模块?
  2. 如果我们将其导入到多个数据库中会发生什么 功能模块又插在根模块中?
  3. 当我们在一个应用程序中有数十个模块,其中一些模块需要HttpClientModule,而我们只想将HttpClientModule导入到这几个模块中时,这是否是一个合适的用例?

解决方案

因此,有两种方法可以在根模块中加载模块,

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

现在,当您在导入数组中加载模块时,该模块提供的所有服务将成为单例服务,即,这些服务的单个实例将在整个应用程序中共享.这些服务的实例是由根注入器在应用程序引导时创建的.

就服务而言,无论是在根模块中加载一个模块还是在多个功能模块中,然后在根模块中加载所有这些功能模块,都将获得单个服务实例./p>

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

但是,您可以查看HttpClientModule 源代码.在声明或导出数组中没有任何内容.它仅提供服务,因此无论是将其加载到功能模块中(将功能模块加载到根模块的imports数组中)还是加载到Root模块中,都将获得服务.因此,只需将其加载到根模块中即可.

现在,当您使用延迟加载来加载功能模块时,延迟加载的模块的确会获得根模块中所有其他模块提供的所有服务,但是它具有自己的注入器,这意味着如果您懒惰地加载HttpClientModule,已加载的模块(已经在根模块中加载),您将得到HttpClientModule提供的所有服务的两个实例,而您并不想这样做.

您可以进一步了解有关forRoot和forChild模式的信息,以解决此类情况.

The docs on HttpClientModule say:

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

  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. Adding it in imports array
  2. Lazy Loading

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.

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.

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.

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.

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

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

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