Question presented to candidate: "We have a checkout page with a cart summary, a quantity stepper inside each line item, and a badge in the top toolbar that shows the item count. Walk me through the ways Vue components can share data and send messages, and which one you would use for each of those pieces."
What a strong answer should cover:
- Props down, events up is the default: the parent passes data with props, the child reports changes with
emit, and the child never mutates a prop. v-modelon a component is shorthand for amodelValueprop plus anupdate:modelValueevent, anddefineModel()(Vue 3.4+) writes both for you.- Slots let a parent send template content into a child, and scoped slots let the child pass data back into that content.
provide/injectskips the layers in between for deep trees (theme, form context, a cart), and providing a ref keeps it reactive.- Template refs +
defineExposefor the rare imperative call (focus, reset), and a store such as Pinia for state shared by distant parts of the app. - Vue 3 removed
$on/$off, so the Vue 2 "event bus" needs an external emitter, and is usually a sign a store or provide/inject fits better.
Clarifying questions expected:
- "Are these components parent and child, or far apart in the tree?"
- "Does the shared state need to survive route changes or be visible in devtools?"
Code / implementation expected: Yes β a child that takes a prop and emits an event (or uses defineModel), plus a provide/inject or store example for the distant badge.