Vue ignores an assignment and warns that the target is readonly
Fix it fast
Most common cause, 1 of 3A child component assigns to its own prop
If the parent should see the change, make it a v-model with defineModel (Vue 3.4 and later).
<!-- Child.vue: parent writes <Child v-model:title="bookTitle" /> -->
<script setup>
const title = defineModel('title')
function rename() {
title.value = 'New title'
}
</script>
<!-- or keep a local copy -->
<script setup>
import { ref } from 'vue'
const props = defineProps(['initialCounter'])
const counter = ref(props.initialCounter)
</script>Short answer
[Vue warn] Set operation on key "x" failed: target is readonly means Vue 3 blocked an assignment, so the value never changed. The target is a prop, an object wrapped in readonly() (often from inject) or a computed such as a store getter. Write to the source instead: defineModel, a provided function or the state. Vue 3.5.
Which cause do you have?
Take the key name from the warning and find where it is assigned. Then look at where that object comes from: defineProps or this from props is cause 1, inject or readonly() is cause 2, a computed or a store getter is cause 3.
grep -rnE "(props|this)\.foo *=|\.foo *=" src/ --include=*.vue --include=*.ts --include=*.jsThen find the line you see in this list and open its fix:
- 1A child component assigns to its own prop
The key is declared in defineProps or the props option, and the line reads props.foo = ... or this.foo = ... - 2The object was wrapped in readonly(), often by provide
The object comes from inject(...) or a composable that returns readonly(state) - 3Code assigns to a computed value or a store getter
The key is a computed() kept inside a reactive object, or a getter of a Pinia store
What does the error look like?
[Vue warn] Set operation on key "foo" failed: target is readonly.[Vue warn] Set operation on key "double" failed: target is readonly.
Cause 1 of 3
A child component assigns to its own prop
You seeThe key is declared in defineProps or the props option, and the line reads props.foo = ... or this.foo = ...
Check
Vue's props guide calls the binding one-way: the parent's value flows down and the child must not overwrite it, so assigning props.foo only prints the readonly warning. Nested fields of an object prop can still be changed, which is why only top-level keys trigger it.
grep -rn "defineProps" src/components/Fix
If the parent should see the change, make it a v-model with defineModel (Vue 3.4 and later). If the prop is only a starting value, copy it into a local ref and change that.
<!-- Child.vue: parent writes <Child v-model:title="bookTitle" /> -->
<script setup>
const title = defineModel('title')
function rename() {
title.value = 'New title'
}
</script>
<!-- or keep a local copy -->
<script setup>
import { ref } from 'vue'
const props = defineProps(['initialCounter'])
const counter = ref(props.initialCounter)
</script>Verify
The warning is gone and the value on screen changes when the code runs.
npm run devConfirmed by vuejs.org
Cause 2 of 3
The object was wrapped in readonly(), often by provide
You seeThe object comes from inject(...) or a composable that returns readonly(state)
Check
readonly() hands out a proxy that refuses every write, nested ones included. The provide/inject guide suggests exactly that wrapper when a provider wants its data safe from injectors, so an injecting component that assigns to it gets the warning.
grep -rn "readonly(" src/Fix
Keep the write inside the provider. Provide a function next to the value and call it from the injecting component.
// provider
const count = ref(0)
function increment() { count.value++ }
provide('counter', { count: readonly(count), increment })
// injector
const { count, increment } = inject('counter')
increment()Verify
Calling the function updates the value everywhere it is shown, with no warning.
npm run devConfirmed by vuejs.org
Cause 3 of 3
Code assigns to a computed value or a store getter
You seeThe key is a computed() kept inside a reactive object, or a getter of a Pinia store
Check
Computed properties are getter-only unless you give them a setter. When such a computed sits inside a reactive object, writing to that key produces the Set operation warning instead of the plain computed one. Pinia getters are computed values of the store, so they cannot be assigned either.
grep -rnE "getters:|computed\(" src/stores/Fix
Write to the state the getter reads, or give the computed both get and set when a writable value really makes sense.
// instead of store.doubleCount = 10
store.count = 5
// writable computed
const fullName = computed({
get: () => `${first.value} ${last.value}`,
set: (v) => { [first.value, last.value] = v.split(' ') },
})Verify
The derived value updates through its source and the console stays quiet.
npm run devConfirmed by vuejs.org
How do I stop it coming back?
- Treat every prop as input only; reach for defineModel whenever a child needs to change a parent value.
- Expose state from composables and providers as readonly plus named functions that do the writing.
- Keep store writes in actions or on state fields, never on getters.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.