在NgModule中使用forRoot的目的是什么? [英] What is purpose of using forRoot in NgModule?

查看:146
本文介绍了在NgModule中使用forRoot的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NgModule中使用forRoot的目的是什么?

What is the purpose of using forRoot in NgModule?

它与AngularJS 1.x中的提供者一样吗?

Is it same as the providers in AngularJS 1.x?

它在延迟加载中如何发挥重要作用?

How does it play the significant role in lazy loading?

TIA.

推荐答案

它与单例有关. 将Angular服务加载到页面1次(单例),并且所有引用都指向该1个实例.

It has to do with singletons. Angular services are loaded onto the page 1 time (singleton) and all references point back to this 1 instance.

存在延迟加载模块尝试创建应为单例的第二个实例的风险,而forRoot()方法是确保应用模块/共享模块/以及任何延迟加载模块的一种方式都使用相同的1个实例(根实例).

There is a risk that a lazy loaded module will try to create a 2nd instance of what should be a singleton, and the forRoot() method is a way to ensure that the app module / shared module / and any lazy loaded module all use the same 1 instance (the root instance).

更多信息复制自: 在Angular 2中提供核心单例服务模块

最好的方法是与提供者一起创建模块.请记住,如果您的服务在共享模块中,则可能会获得它的多个实例.那么最好的办法是使用Module.forRoot配置模块.

The best approach is to create module with providers. Keep in mind that if your service is in shared module, you may get multiple instances of it. Best idea then is to configure module with Module.forRoot.

按照惯例,forRoot静态方法同时提供和配置服务.它需要一个服务配置对象并返回ModuleWithProviders.

By convention, the forRoot static method both provides and configures services at the same time. It takes a service configuration object and returns a ModuleWithProviders.

仅在根应用程序模块AppModule中调用forRoot.在任何其他模块中(尤其是在延迟加载的模块中)调用它与意图相反,并且可能会产生运行时错误.

Call forRoot only in the root application module, AppModule. Calling it in any other module, particularly in a lazy loaded module, is contrary to the intent and is likely to produce a runtime error.

记住要导入结果;请勿将其添加到任何其他@NgModule列表中.

Remember to import the result; don't add it to any other @NgModule list.

如果模块同时提供了提供者和声明(组件,指令,管道),则将其加载到子注入器(例如路由)中,将复制提供者实例.提供程序的重复会引起问题,因为它们会遮盖根实例,这可能意味着是单例.因此,Angular提供了一种将提供者从模块中分离出来的方法,以便可以将具有提供者的相同模块导入到具有提供者的根模块中,而将没有提供者的子模块导入到根模块中.

If a module provides both providers and declarations (components, directives, pipes) then loading it in a child injector such as a route, would duplicate the provider instances. The duplication of providers would cause issues as they would shadow the root instances, which are probably meant to be singletons. For this reason Angular provides a way to separate providers out of the module so that same module can be imported into the root module with providers and child modules without providers.

在模块上创建用于Root()的静态方法(按照约定). 如下将提供程序放入forRoot方法中. 为了更加具体,请以RouterModule为例. RouterModule需要提供路由器服务以及RouterOutlet指令.根应用程序模块必须导入RouterModule,以便该应用程序具有一个Router,并且该应用程序至少具有一个RouterOutlet.它还必须由各个路由组件导入,以便它们可以将RouterOutlet指令放入其子路由模板中.

Create a static method forRoot() (by convention) on the module. Place the providers into the forRoot method as follows. To make this more concrete, consider the RouterModule as an example. RouterModule needs to provide the Router service, as well as the RouterOutlet directive. RouterModule has to be imported by the root application module so that the application has a Router and the application has at least one RouterOutlet. It also must be imported by the individual route components so that they can place RouterOutlet directives into their template for sub-routes.

如果RouterModule没有forRoot(),则每个路由组件都会实例化一个新的Router实例,这将破坏应用程序,因为只能有一个Router.因此,RouterModule具有RouterOutlet声明,以便可以在任何地方使用,但是Router提供程序仅位于forRoot()中.结果是,根应用程序模块导入了RouterModule.forRoot(...)并获得了一个路由器,而所有路由组件均导入了不包括路由器的RouterModule.

If the RouterModule didn’t have forRoot() then each route component would instantiate a new Router instance, which would break the application as there can only be one Router. For this reason, the RouterModule has the RouterOutlet declaration so that it is available everywhere, but the Router provider is only in the forRoot(). The result is that the root application module imports RouterModule.forRoot(...) and gets a Router, whereas all route components import RouterModule which does not include the Router.

如果您有一个同时提供提供程序和声明的模块,请使用此模式将它们分开.

If you have a module which provides both providers and declarations, use this pattern to separate them out.

这篇关于在NgModule中使用forRoot的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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