Vue · Middle · Question 15 of 75

Why use an InjectionKey symbol instead of a string for provide/inject?

export const ThemeKey: InjectionKey<Ref<string>> = Symbol("theme");
provide(ThemeKey, theme);
const t = inject(ThemeKey); // Ref<string> | undefined

Explanation

Put keys in a shared file. inject returns T | undefined unless you pass a default value.

Further reading

Problem with a question

Vue · Middle · question 15

Test yourself

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

Take this quiz