角度单例服务 [英] Angular singleton service
问题描述
据我了解,使用 providedIn:'root'
和将您的提供程序添加到模块的 providers
数组之间的唯一区别是摇树.目前,如果我添加 providedIn:'root'
,我的应用程序可以正常工作,但是如果我将其删除并将其添加到模块的providers数组中,则会得到 StaticInjectorError
说找不到供应商.有没有人看到或了解为什么会发生这种情况?从文档中,我认为将其添加到providers数组应该可以使其工作
From my understanding the only difference between using providedIn: 'root'
and adding your provider to the providers
array of the module is for tree shaking. Currently my application works fine if I add providedIn: 'root'
it works fine, but if I remove that and add it to the providers array of the module I get the StaticInjectorError
saying it cant find the provider. Has anyone seen this or have an understanding of why this would happen? From the docs I believe that adding it to the providers array should allow this to work
推荐答案
该错误是不言自明的.如果您执行 providedIn:'root'
,则您的服务将在RootInjector上注册.因此,基本上可以在整个App中使用它,而不必手动将其添加到要在其中使用此服务的每个模块的 providers
数组.
The error is pretty self-explanatory. IF you do providedIn: 'root'
, then your service gets registered on the RootInjector. And hence it's basically available to use throughout the App without having to manually add it to the providers
array of each of the module that you want to use this service in.
但是现在您已经从 @Injectable
装饰器中删除了 providedIn:'root'
,它仅适用于将其添加到这些模块的 providers
数组.否则将对导入将服务添加到 providers
数组的模块的模块可用.
But now that you have removed the providedIn: 'root'
from the @Injectable
decorator, it will only be available to the Modules where you are adding it to the providers
array of those modules. Or it would be available to the modules who are importing the modules that have the service added to the providers
array.
这是 示例StackBlitz 可帮助您更好地理解这一点.
Here's a Sample StackBlitz to help you understand this in a better way.
仅描述一下:
- 我那里有3个模块:
AppModule
,NewModule
和NotImportedModule
. - 我将使用在
AppModule
的AppComponent
NewModule 和NotImportedModule
中编写的服务>. 将 -
NewModule
的SampleService
添加到NewModule
和NewModule的
添加到provides
数组中AppModule
的imports
数组中.这就是为什么我能够在AppComponent
中使用SampleService
的原因. -
NotImportedModule
未添加到AppModule
的imports
数组中,但AnotherService
是providedIn:"root"
.因此,我可以在AppModule
中使用它.
- I have 3 Modules in there:
AppModule
,NewModule
, andNotImportedModule
. - I'll be using the services written in the
NewModule
and theNotImportedModule
in theAppModule
'sAppComponent
. NewModule
'sSampleService
is added to theprovides
array of theNewModule
and theNewModule
is added to theimports
array of theAppModule
. And that's why I'm able to use theSampleService
in theAppComponent
.NotImportedModule
is not added to theimports
array of theAppModule
but theAnotherService
isprovidedIn: 'root'
. Hence I'm able to use it in theAppModule
.
这意味着 AnotherService
将可在整个App中使用,因为它已在RootInjector上注册,因为我们在其上使用了 providedIn:'root'
.
Which would mean that AnotherService
would be available to be used throughout the App since it is registered on the RootInjector, since we used providedIn: 'root'
on it.
但是由于 SampleService
不是 provideredIn:'root'
,而是被添加到 NewModule
providers 数组中>要在 AppModule
中使用它,我们必须将 NewModule
添加到 AppModule
的 imports
数组中.
But since SampleService
was NOT providedIn: 'root'
but was added to the providers
array of the NewModule
to use it in the AppModule
, we had to add the NewModule
to the imports
array of the AppModule
.
希望这个例子更有意义.
Hope it makes more sense with this example.
这篇关于角度单例服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!