Question presented to candidate:
"We want a $formatPrice helper usable in every template, the API base URL available to every component, and one place that logs all uncaught component errors. How do you set up global properties and app-wide config in Vue 3, and how does that work with <script setup> and TypeScript?"
What a strong answer should cover:
app.config.globalProperties.$x = ...makes$xavailable asthis.$xand in every template; it replaces Vue 2'sVue.prototype.$x.- In
<script setup>there is nothis, so script code should get shared values fromapp.provide()+inject(); reaching throughgetCurrentInstance()works but is discouraged. - A component's own data, props or methods shadow a global property of the same name.
- Global properties need a TypeScript augmentation of
ComponentCustomPropertiesin a module file (withexport {}). app.configalso holdserrorHandler,warnHandler(dev only),performance,idPrefix,throwUnhandledErrorInProductionandcompilerOptions(runtime compiler builds only).- Everything is per app instance, set before
mount(); in SSR it must be set per request.
Clarifying questions expected:
- "Is the codebase Options API,
<script setup>, or both? That decides between globalProperties and provide." - "Is there SSR? App config then has to be created per request."
Code / implementation expected: Yes β a main.ts that sets a global property, an app-level provide and an error handler, and a typed augmentation.