Question presented to candidate: "Computed properties are usually read-only. Can you make one writable in Vue 3? Show me the syntax, explain what actually happens on assignment, and give me a case where you would really use it."
What a strong answer should cover:
- Yes: pass an object with
getandsettocomputed({ get, set }); it returns aWritableComputedRef. - The setter stores nothing. It must write back to the source state; the next read re-runs the getter, so the getter always wins.
- Writing to a getter-only computed logs the dev warning
Write operation failed: computed value is readonlyand is ignored. - Real uses:
v-modelon a prop (get from the prop, set by emittingupdate:modelValue), two views of one source (Celsius and Fahrenheit), parsing and clamping input, and bindingv-modelto store state. - Since Vue 3.4
defineModel()covers the prop-plus-emit case in<script setup>; the getter also receives the previous value (3.4), and getter and setter types may differ (3.5). - Keep setters synchronous and side-effect free apart from writing the source.
Clarifying questions expected:
- "Is this for a component's own state, or to support
v-modelon a child?" - "Which Vue version?
defineModelchanges the answer from 3.4 on."
Code / implementation expected: Yes — a get/set computed bound with v-model, ideally a child component that supports v-model through one.