Angular · Middle · Question 23 of 75

Why use an InjectionToken?

export const API_URL = new InjectionToken<string>("API_URL");
// providers: [{ provide: API_URL, useValue: "https://api.example.com" }]
// usage: private apiUrl = inject(API_URL);

Explanation

Interfaces disappear at runtime, so they can't be DI tokens. An InjectionToken is a unique runtime key that also carries the TypeScript type.

Further reading

Problem with a question

Angular · Middle · question 23

Test yourself

Take the Angular · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz