Giter VIP home page Giter VIP logo

Comments (3)

dadaguai-git avatar dadaguai-git commented on May 18, 2024 1

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
demo_20240126174341

from core.

soyamore avatar soyamore commented on May 18, 2024 1

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.

shengslogar avatar shengslogar commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.