Comments (3)
If you want to access the owner component's DOM in a watcher callback after Vue has updated it, you need to specify the flush: post
from core.
You can define msg as prop in the Child component and emit the value like the example below :
<script setup>
defineProps('msg', {type: String});
defineEmits(['update:value'])
</script>
<template>
<input :value="msg" @input="$emit('update:value', $event.target.value) />
</template>
For the parent component
<script setup>
import { ref, watch } from 'vue'
import ChildComp from './ChildComp.vue';
const msg = ref('Hello World!')
const updateMsgValue = (newVal) => {
msg.value = newVal
}
</script>
<template>
<input v-model="msg" />
<ChildComp :msg="msg" @update:value="updateMsgValue" />
</template>
from core.
Thanks, @dadaguai-git, you nailed it! I had no idea that option existed. (I should probably read through the docs again.)
I'm not sure why this works, but my guess is a regular watcher is somehow shortcutting the normal event lifecycle, probably between the child @update:model-value
event and the value that's getting passed back down, although I'm still not clear as to why this is needed with a child component, but not inside the parent component.
(Additionally, if you inspect the value present at the child component in the repro linked above, the correct value is being propagated downwards, but not being set at the native <input>
level. That strikes me as odd.)
nextTick
appears to behave identically to { flush: 'post' }
. In both cases, msg.value
gets updated twice for each keystroke, as expected:
<script setup>
import { ref, nextTick } from 'vue'
import ChildComp from './ChildComp.vue';
const msg = ref('Hello World!')
async function handleUpdate() {
await nextTick();
msg.value = ''
}
</script>
<template>
<ChildComp v-model="msg" @update:model-value="handleUpdate" />
</template>
Closing this out as seemingly intended behavior, or at least one with a built-in workaround, but any further clarification would be appreciated.
from core.
Related Issues (20)
- Custom elements that use vue components within have slot elements removed HOT 1
- v-model causes different behavior to :value="value" @input="value = $event.target.value"
- [@vue/compiler-sfc] Failed to resolve import source HOT 1
- The License comment in the first lines got lost with vue3 HOT 2
- The arrow function is used in vuex, causing computed to fail to get the state in response HOT 1
- Uncaught (in promise) Maximum recursive updates exceeded in component <RouterView> HOT 7
- Reactivity maybe be broken with nested computed and vue-router HOT 9
- Default value of defineModel is not handled as a local ref HOT 1
- Donating Funds to Vuejs/Core HOT 1
- Generic props extending boolean should apply Boolean casting HOT 1
- Why is the length of the slot 0? HOT 1
- App crashes when using router-view in a top-level route target when there's a transition in the top level RouterView's slot
- keep-alive组件里面的变量storageContainer存在内存泄漏 HOT 4
- The register mismatch between display value in input and reactive variable HOT 9
- [SSR] Hydration error using v-bind in scoepd styles HOT 2
- Nested calls to `runWithContext` resets the app for the rest of the top `runWithContext` call
- @vue/compiler-sfc 单独在vscode插件中使用时,无法正确打包 HOT 2
- [Bug][Vue SFC Playground] Playground has a memory leak problem HOT 1
- `<select>` with `v-model` didn't update DOM after mutating data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.