简体   繁体   English

Angular 提供了根和根模式

[英]Angular providedIn root and forRoot pattern

Hi what is the difference between Angular's providedIn: root at service level and using the forRoot() module pattern?您好 Angular 的providedIn: root在服务级别和使用forRoot()模块模式有什么区别? I have already seen this answer Angular service providedIn VS forRoot which says there is no difference.我已经看到这个答案Angular service providedIn VS forRoot说没有区别。

  1. Does this stand true even if we are creating a library which gives a service?即使我们正在创建一个提供服务的图书馆,这是否成立?
  2. Why do we need to use forRoot pattern with router module then?那么为什么我们需要在路由器模块中使用forRoot模式呢? Why isn't router service provided in root?为什么不在 root 中提供路由器服务? Any compatibility related reason?任何与兼容性相关的原因?

The preferred way to provide a service in a module, do it inside the service,在模块中提供服务的首选方式,在服务内部进行,

@Injectable({ providedIn: 'any', })

With providedIn: 'any', all eagerly loaded modules share a singleton instance;使用 providedIn: 'any',所有急切加载的模块共享一个 singleton 实例; however, lazy loaded modules each get their own unique instance然而,延迟加载的模块每个都有自己独特的实例

OR要么

u can choose the following way:您可以选择以下方式:

providedIn: YourModule

into your service进入你的服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM