简体   繁体   English

在'root','platform','any' w.r.t providedIn 中,在 Angular 的这种情况下应该首选哪一个?

[英]Out of 'root', 'platform', 'any' w.r.t providedIn, which one should be preferred in which case in Angular?

Out of 'root', 'platform', 'any' w.r.t providedIn, which one should be preferred in which case in Angular?在'root','platform','any' w.r.t providedIn 中,在 Angular 的这种情况下应该首选哪一个?

https://angular.io/api/core/Injectable https://angular.io/api/core/Injectable

'root' : The application-level injector in most apps. 'root' :大多数应用程序中的应用程序级注入器。
'platform' : A special singleton platform injector shared by all applications on the page. 'platform' :页面上所有应用程序共享的特殊 singleton 平台注入器。
'any' : Provides a unique instance in every module (including lazy modules) that injects the token. 'any' :在注入令牌的每个模块(包括惰性模块)中提供唯一的实例。

In which kind of cases would we want unique instance in every module rather than letting the root deal with all?在哪种情况下,我们希望每个模块中都有唯一的实例,而不是让根处理所有的?

What is an example use case of platform?什么是平台的示例用例?

So, while not sure I can tailor this answer to any specific use case, my initial impression of the new options here is that they aren't tremendously useful except in edge cases where the developer isn't properly using Angular.因此,虽然不确定我是否可以针对任何特定用例定制这个答案,但我对这里的新选项的初步印象是,它们并不是非常有用,除非在开发人员没有正确使用 Angular 的边缘情况下。 Based on a quick read through of some articles, including this one and this one , it appears that the new options give the developer some added flexibility to misdesign their application and introduce strange and difficult-to-track bugs.根据对一些文章的快速阅读,包括这篇文章和这篇文章,新选项似乎为开发人员提供了一些额外的灵活性来错误设计他们的应用程序并引入奇怪且难以跟踪的错误。

Angular's DI framework allows you to define the scope for your services (remember that services are where your application's state lives ). Angular 的 DI 框架允许您为您的服务定义 scope(请记住, 服务是您的应用程序的 state 所在的位置)。 The option 'root' declares that only one instance of your service will be created for the whole application, so the whole application will share whatever state the service holds.选项'root'声明将为整个应用程序只创建一个服务实例,因此整个应用程序将共享服务所拥有的任何 state。 The alternative was to specify a specific module in the providedIn choice or, more commonly, to specify a provides: section of your module declaration.另一种方法是在providedIn选项中指定一个特定的模块,或者更常见的是,在你的模块声明中指定一个provides:部分。 In that case, you either need to import your service by importing a module, or you need to explicitly declare that it is contained within one module.在这种情况下,您要么需要通过导入模块来导入服务,要么需要明确声明它包含在一个模块中。

The key with the old approach is that, if you messed it up, you would get an easy-to-track error at runtime.旧方法的关键是,如果你把它搞砸了,你会在运行时得到一个易于跟踪的错误。

The new approach allows you to specify a bit more ambiguity.新方法允许您指定更多的歧义。 'any' means what it says - every module needing a particular service will generate its own. 'any'意味着它所说的 - 每个需要特定服务的模块都会生成自己的。 This will almost certainly trip up beginning and intermediate level developers, who are not familiar with the symptoms of inconsistent state in their programs.这几乎肯定会绊倒初级和中级开发人员,他们不熟悉程序中不一致的 state 的症状。 Conversely, 'platform' will allow multiple applications loaded into the same window (does this include applications in different iFrames?) to share the same state.相反, 'platform'将允许加载到同一个 window 的多个应用程序(这包括不同 iFrame 中的应用程序吗?)共享同一个 state。 At best, this option is unnecessary (only one Angular application should ever be loaded at a time, unless you're talking about Angular Elements ), and at worst, it runs the risk of making your application difficult or impossible to test.充其量,此选项是不必要的(一次只应加载一个 Angular 元素,除非您在谈论Angular 元素),在最坏的情况下,它会冒使您的应用程序难以或无法测试的风险。

Bottom line: you shouldn't find yourself using these new choices, unless you made a pretty big mistake earlier in your architecture process, or you're writing a library of some sort.底线:你不应该发现自己使用这些新选择,除非你在架构过程的早期犯了一个相当大的错误,或者你正在编写某种库。

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

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