Allow injection tokens to be provided in the same way as services #49807
Labels
area: core
Issues related to the framework runtime
core: di
feature: under consideration
Feature request for which voting has completed and the request is now under consideration
feature
Issue that requests a new feature
Milestone
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Unlike services, injection tokens cannot be provided at the component/route level without using
useFactory/Value/Class.The
BAR_TOKENwill be provided at the root level even if we don't useprovidedIn: 'root'. If we want to provideBAR_TOKENat the component level, it cannot be provided in the following way:To make it work, we need to explicitly specify its factory, although we have already defined
factoryin theBAR_TOKENdefinition:Proposed solution
Unlike tokens, with services, we need to add
@Injectable({ providedIn: 'root' })to provide them at the root level. I'd like to have the same behavior with injection tokens:Alternatives considered
We can use
inject...functions instead of injection tokens:However, by using this approach, the
inject...function body will be executed every time when it's invoked. On the other hand, if we are able to use injection tokens in the way that is described in the previous section, the injection token factory will be executed only the first time when it's injected.The text was updated successfully, but these errors were encountered: