Answer the question
In order to leave comments, you need to log in
When should UseExistingProvider be used?
As for DI in Angular, I have the only question that torments me for a long time, I don’t understand the purpose of the useExisting provider and how it differs from useClass, and also where to apply it. I have a very vague idea about it, at the moment I have the following thoughts on useExisting:
!!! Everything below is false information and nonsense, but I want you to understand what my idea of \u200b\u200bhe is.
1) useExisting does not create a new dependency instance, but creates a link to it under another class that serves as its replacement (alias).
2) useExisting should be used together with useClass, i.e. the class that is specified in { provide: SomeClass } must be both the token itself and the source of the dependency, and must be injected at a hierarchy level somewhere higher than useExisting, or at the same level, but ultimately it must still be injected as { provide: SomeClass, useClass: SomeClass }
3) The alias class that we inject must fully match the interface of the original class we are going to replace with.
Answer the question
In order to leave comments, you need to log in
1) useExisting does not create a new dependency instance, but creates a link to it under another class that serves as its replacement (alias).
providers: [{
provide: 'doc',
useExisting: DOCUMENT
}]
2) useExisting should be used together with useClass
3) the alias class that we are implementing must fully comply with the interface of the original class that we are going to replace with.
When should UseExistingProvider be used?
HTTP_INTERCEPTORS
, form directives inject NG_VALIDATORS
, bootstrap injects APP_INITIALIZER
, etc. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question