Question presented to candidate:
"We are building a design-system SearchBox and a NameFields component that edits a first and last name. Parents should be able to write v-model on them like on a native input. What contract does the component have to implement, and how do you support two models on one component?"
What a strong answer should cover:
- On a component,
v-model="x"expands to:modelValue="x"plus@update:modelValue="v => x = v": a prop down, an event up. - Named models:
v-model:title="t"uses atitleprop and anupdate:titleevent, which is how one component supports several models. - The child must never mutate the prop; it emits the new value and the parent assigns it (one-way data flow).
- Forwarding to an inner
<input>: a writable computed (get reads the prop, set emits) or, since 3.4,defineModel(). - Vue 2 differences: the default was
value+input, and.syncis replaced byv-model:arg. - Modifiers travel as a
modelModifiersprop.
Clarifying questions expected:
- "Which Vue version is the design system on? From 3.4 I would use
defineModel." - "Does the parent always bind the value, or should the component also work uncontrolled?"
Code / implementation expected: Yes. A component implementing the prop + emit contract, ideally with a named second model.