Proposal: propsToModels() #725
Replies: 4 comments 17 replies
-
It seems that you are looking for |
Beta Was this translation helpful? Give feedback.
-
<script setup>
import GrandChild from "./GrandChild.vue";
const model = defineModel();
</script>
<template>
<div>
<h3>Component</h3>
a: <GrandChild v-model="model.a" /><br />
b: <GrandChild v-model="model.b" />
</div>
</template> This doesn't trigger the emit and actually mutates the prop |
Beta Was this translation helpful? Give feedback.
-
It seems that you need https://vueuse.org/core/useVModels/ |
Beta Was this translation helpful? Give feedback.
-
i made a similar proposal, with a different api that leverages proxies and directives to solve the same thing |
Beta Was this translation helpful? Give feedback.
-
The idea is that
defineProps()
should actually return a mutable copy of the actual props, with listeners on them automatically emitting when the props copies are modified or when the original props are modified.The API would be something like
propsToModels()
This is a rough implementation and can be improved a lot
This solves the issue and need of having to create a local copy of the prop and listening to its mutations in order to update the real prop. It's particularly useful when dealing with nested v-model props and when props are objects.
An example
Now
workspace
can be deeply used with v-model by child components and so on. Mutatingworkspace
will trigger an emit which will update the realworkspace
prop. If theworkspace
prop is mutated by parent or other component, the local copy will be updated too and remain mutable.Beta Was this translation helpful? Give feedback.
All reactions