Visitar URL original
Allow injection tokens to be provided in the same way as services · Issue #49807 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20260227035126/https://github.com/angular/angular/issues/49807
Skip to content

Allow injection tokens to be provided in the same way as services #49807

@markostanimirovic

Description

@markostanimirovic

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: difeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under consideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions