Skip to content
Advertisement

How to mutate props in Vue3 and have the parent computed setter called

I know this is an anti pattern, but it would simplify things a lot. And furthermore I’m just curious if it’s possible. When I call test1() I get the following warning: Set operation on key "items" failed: target is readonly. and nothing else happens. When I call test2() items change within the child AND the parent, BUT the computed setter ist not called. Why?

Why is the computed setter not called? Is it possible to mutate the props within the child and still have the computed setter called?

I’ve got the following parent component:

JavaScript

And this is the child component:

JavaScript

Advertisement

Answer

You’re likely looking for v-model:items="items".

The gist:

parent.vue:

JavaScript

child.vue:

JavaScript

Note: both items.pop() and items.push() will mutate the value in both parent and child, but they won’t trigger an assignment.

For that, you have to:

JavaScript

Docs here

Working example here


Technically,

JavaScript

is shorthand for:

JavaScript

In you don’t specify the child variable name (in our case items), it defaults to modelValue.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement