I am struggling to implement two-way binding because I can't determine if the model ref
is being changed by the parent or child component.
Using watch
captures all changes without any indication of the source of the change.
<script setup>
// Parent.vue
const doc = ref('');
setTimeout(() => {
// Update this ref AND trigger watch in Child.vue
doc.value = 'new value from parent';
}, 2000);
</script>
<template>
<Child v-model:doc="doc" />
</template>
<script setup>
// Child.vue
const doc = defineModel('doc');
setTimeout(() => {
// DO NOT TRIGGER WATCH ON THIS ONE!
// But still update parent `doc` ref
doc.value = 'new inner value';
}, 3000);
watch(doc, newValue => {
// Catch `new value from parent` but not `new inner value`
});
</script>