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

查看:30
本文介绍了在 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 需要提供 Router 服务,以及 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(...) 并得到一个 Router,而所有路由组件导入 RouterModule 不包括 Router.

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天全站免登陆