-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Description
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.
const BAR_TOKEN = new InjectionToken('Bar', {
factory() {
const foo = inject(FooService);
return foo.bar;
}
});The BAR_TOKEN will be provided at the root level even if we don't use providedIn: 'root'. If we want to provide BAR_TOKEN at the component level, it cannot be provided in the following way:
@Component({
providers: [BAR_TOKEN], // this will not work even if we defined `factory` for the `BAR_TOKEN`
})
export class BarComponent {}To make it work, we need to explicitly specify its factory, although we have already defined factory in the BAR_TOKEN definition:
@Component({
providers: [{ provide: BAR_TOKEN, useFactory: () => inject(FooService).bar }],
})
export class BarComponent {}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:
const BAR_TOKEN_1 = new InjectionToken('Bar', {
providedIn: 'root',
factory() {
const foo = inject(FooService);
return foo.bar;
}
}); // BAR_TOKEN_1 is provided at the root level (no changes in this case)
const BAR_TOKEN_2 = new InjectionToken('Bar', {
factory() {
const foo = inject(FooService);
return foo.bar;
}
});
// BAR_TOKEN_2 is not provided anywhere by default,
// similar to `@Injectable()` that doesn't contain `{ providedIn: root }` config.
// However, `InjectionToken` with a factory can be provided in the following way:
@Component({
providers: [BAR_TOKEN_2]
})
export class BarComponent {}Alternatives considered
We can use inject... functions instead of injection tokens:
export function injectBar() {
const foo = inject(FooService);
return foo.bar;
}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.

