How to handle watch() with @input in vue

I want to handle a v-model change based on an @input variable update, but I’m facing issues with the order of “happenings” in vue. Here is a simplified version of my issue:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup>
import { ref, watch } from 'vue'
const foo = ref(1)
const bar = ref(1)
const lorem = ref(false)
const handleInput = (val) => {
bar.value = val
}
watch(foo, (newFoo) => {
if (bar.value === 2) {
lorem.value = true
}
})
</script>
<template>
<input v-model="foo" @input="handleInput(2)"><br>
{{foo}}<br>
{{bar}}<br>
{{lorem}}
</template>
</code>
<code><script setup> import { ref, watch } from 'vue' const foo = ref(1) const bar = ref(1) const lorem = ref(false) const handleInput = (val) => { bar.value = val } watch(foo, (newFoo) => { if (bar.value === 2) { lorem.value = true } }) </script> <template> <input v-model="foo" @input="handleInput(2)"><br> {{foo}}<br> {{bar}}<br> {{lorem}} </template> </code>
<script setup>
import { ref, watch } from 'vue'

const foo = ref(1)
const bar = ref(1)
const lorem = ref(false)

const handleInput = (val) => {
  bar.value = val
}

watch(foo, (newFoo) => {
  if (bar.value === 2) {
    lorem.value = true
  }
})
</script>

<template>
  <input v-model="foo" @input="handleInput(2)"><br>
  {{foo}}<br>
  {{bar}}<br>
  {{lorem}}
</template>

It seems, that the watcher hits before the handleInput method was executed.

If I change the value of foo in the input field, the lorem value will be set to true if I change the value of foo a second time, only. I want to set it to true immediately.

I tried to implement nextTick() and flush: 'post', but it does not have the expected result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup>
import { ref, nextTick, watch } from 'vue'
const foo = ref(1)
const bar = ref(1)
const lorem = ref(false)
const handleInput = (val) => {
nextTick(() => {
bar.value = val
})
}
watch(foo, async (newFoo) => {
await nextTick()
if (bar.value === 2) {
lorem.value = true
}
}, { flush: 'post' })
</script>
<template>
<input v-model="foo" @input="handleInput(2)"><br>
{{foo}}<br>
{{bar}}<br>
{{lorem}}
</template>
</code>
<code><script setup> import { ref, nextTick, watch } from 'vue' const foo = ref(1) const bar = ref(1) const lorem = ref(false) const handleInput = (val) => { nextTick(() => { bar.value = val }) } watch(foo, async (newFoo) => { await nextTick() if (bar.value === 2) { lorem.value = true } }, { flush: 'post' }) </script> <template> <input v-model="foo" @input="handleInput(2)"><br> {{foo}}<br> {{bar}}<br> {{lorem}} </template> </code>
<script setup>
import { ref, nextTick, watch } from 'vue'

const foo = ref(1)
const bar = ref(1)
const lorem = ref(false)

const handleInput = (val) => {
  nextTick(() => {
    bar.value = val
  })
}

watch(foo, async (newFoo) => {
  await nextTick()
  if (bar.value === 2) {
    lorem.value = true
  }
}, { flush: 'post' })
</script>

<template>
  <input v-model="foo" @input="handleInput(2)"><br>
  {{foo}}<br>
  {{bar}}<br>
  {{lorem}}
</template>

Here is a Vue Playground showing my issue.

5

In the Vue’s code there are “jobs” executed seemingly as microtasks and the DOM update following.

nextTick():

A utility for waiting for the next DOM update flush.

In our case the changing of an input value doesn’t queue any DOM update so nextTick() is scheduled immediately as a microtask. Using 2 await nextTick(); also doesn’t help… Neither {flush:post}

So the solution is to use more a radical way to postpone the watch’s code, setTimeout() comes to mind, but I’ve tried requestAnimationFrame and it works, I like it better because it allows execution right after all microtasks and before the page repaint.

Playground

But actually you don’t need this trick, just place all of your logic inside handleInput:

Playground

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup>
import { ref, watch , reactive, nextTick} from 'vue'
const foo = ref(1)
const bar = ref(1)
const lorem = ref(false)
const log = reactive([]);
const handleInput = (val) => {
bar.value = val
lorem.value = bar.value === 2;
log.push(`bar: ${val} lorem: ${lorem.value} foo: ${foo.value}`);
}
</script>
<template>
<input v-model="foo" @input="handleInput(2)"><br>
<input v-model="foo" @input="handleInput(1)"><br>
{{foo}}<br>
{{bar}}<br>
{{lorem}}
<div v-for="line in log">
{{line}}
</div>
</template>
</code>
<code><script setup> import { ref, watch , reactive, nextTick} from 'vue' const foo = ref(1) const bar = ref(1) const lorem = ref(false) const log = reactive([]); const handleInput = (val) => { bar.value = val lorem.value = bar.value === 2; log.push(`bar: ${val} lorem: ${lorem.value} foo: ${foo.value}`); } </script> <template> <input v-model="foo" @input="handleInput(2)"><br> <input v-model="foo" @input="handleInput(1)"><br> {{foo}}<br> {{bar}}<br> {{lorem}} <div v-for="line in log"> {{line}} </div> </template> </code>
<script setup>

import { ref, watch , reactive, nextTick} from 'vue'

const foo = ref(1)
const bar = ref(1)
const lorem = ref(false)
const log = reactive([]);

const handleInput = (val) => {
  bar.value = val
  lorem.value = bar.value === 2;
  log.push(`bar: ${val} lorem: ${lorem.value} foo: ${foo.value}`);
}

</script>

<template>
  <input v-model="foo" @input="handleInput(2)"><br>
  <input v-model="foo" @input="handleInput(1)"><br>
  {{foo}}<br>
  {{bar}}<br>
  {{lorem}}
  <div v-for="line in log">
    {{line}}
  </div>
</template>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật