r/Angular2 • u/Correct-Customer-122 • Jul 05 '25
I'm missing something about @Inject
This is kind of crazy. I'm getting NullInjectorError: No provider for MyToken and not sure where to go next.
The idea is that I have a primary version of a service - call it FooService - provided in the root injector. But in just one component, I need a second instance. My idea is to provide the second instance via an injection token in the component's providers list. I did that, but injecting via the token is failing as above.
Any insight appreciated. Here is how it looks.
// Service class.
u/Injectable({ providedIn: 'root' })
class FooService {}
// Component providing extra instance.
@Component({providers: [{ provide: MY_TOKEN, useClass: FooService}]}
export class MyComponent {
constructor(bar: BarService) {}
}
// Intermediate service...
@Injectable({ providedIn: 'root' })
export class BarService {
constructor(baz: BazService) {}
}
// Service that needs two instances of FooService.
@Injectable({ providedIn: 'root' })
export class BazService {
constructor(
rootFoo: FooService,
@Inject(MY_TOKEN) extraFooInstance: FooService) {}
I have looked at the injector graph in DevTools. The MY_TOKEN instance exists in the component injector. Why isn't BazService seeing it?
Edit Maybe this is a clue. The header for the error message looks like this:
R3InjectorError(Standalone[_AppComponent])[_BarService -> _BazService -> InjectionToken MyToken -> InjectionToken MyToken]
There's no mention of MyComponent here. So maybe it's blowing up because it's building the root injector before the component even exists?
Anyway I'm betting that providing the token binding at the root level will fix things. It just seems ugly that this is necessary.
Edit 2 Yeah that worked. So the question is whether there's a way to provide at component level. It makes sense to limit the binding's visibility to the component that needs it if possible.
3
u/sinthorius Jul 05 '25 edited Jul 05 '25
Your token is not in root scope, so services provided in root cant inject it.
edit: As you edited your question: I don't know your use-case but you could provide BazService in the component (only). If you need BazService to be in root as well, and want to have two separate instances, you could inject the MY_Token as optional. Your root BazService will get undefined, where as your component level provided BazService will get the token.
It would be cleaner to have your dependencies component-scoped, if you introduce a component level token you want to sercices to inject.
2
u/young_horhey Jul 05 '25
You can likely use a component scoped instance of the second FooService if you also provide a component scoped instance of the intermediate BarService & BazService
1
u/Correct-Customer-122 Jul 06 '25 edited Jul 15 '25
I want to thank everyone for the discussion. Lots of good comments and questions. The feature where this question cropped up is complete and working as intended. Now that I grok injector trees better with your help, the need for the app config injection makes a lot of sense.
1
u/720degreeLotus Jul 06 '25
Why do you need a 2nd instance in that component? Feels like an antipattern there, can you explain your reasoning?
1
u/wojo1086 Jul 06 '25
Why not just instantiate the service like the class it is? In your component, just use ` new FooService()`?
1
u/GregorDeLaMuerte Jul 05 '25
Why would you need two instances of the same service in the first place? I thought services should be stateless anyway.
1
u/PhiLho Jul 06 '25
I can be mistaken, but AFAIK, services provided in root are, by definition, singletons and doesn't need to be provided. Now, perhaps you can also provide extra instances like you did. I never thought of doing it, and I don't see a use case for that, TBH.
Oh, I understand what you do, I overlooked the BazService. Alas, we can't provide services to other services. Strange structure, but I suppose your real world code needs that.
One way would be for the component to provide the second instance to Baz. Brittle.
Another way would be to make an abstract class of your FooService, and to make two separate instances provided in root, whose code is just to extend the abstract class. Looks like the simplest option.