What is the difference between providedIn any and root

跟風遠走 提交于 2020-02-28 09:51:12

问题


In Angular 9 the injectable decorator option providedIn has a new value called any. What is the difference between root and any?

Is a service considered a singleton in the case that I use any?

@Injectable({providedIn: 'any'})
class UsefulService {
}

回答1:


The difference between the root and any as per offical documentation :

  • root : The application-level injector in most apps.

  • platform : A special singleton platform injector shared by all applications on the page.

  • any : The NgModule injector that receives the resolution.

For more details please refer this article.

Is a service considered a singleton in the case that I use any? - No




回答2:


angular 9 introduce new option for injectable decorator ProvidedIn in addition to the previous root and module options, now we have two additional options platform , any

🔵 root— This tells Angular to provide the service in the application root level and the service will be created once (singleton service ) and provide the same instance in every module that injects the token.

🔵 any— Provides a unique instance in every module (including lazy modules) that injects the token.

🔵 platform— Specifying providedIn: 'platform' makes the service available in a special singleton platform injector that is shared by all applications on the page.

a detailed look at Angular's 'root' and 'any' provider scopes



来源:https://stackoverflow.com/questions/59892369/what-is-the-difference-between-providedin-any-and-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!