You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue.js为何滞后一个更新周期?为何需用jQuery获取当前值?

Why does Vue's v-model lag one update cycle in the change event, forcing me to use jQuery?

Hey there, let's break down why you're hitting this confusing behavior and how to fix it without leaning on jQuery—you definitely don't need it here!

The Root Cause

The issue boils down to timing between native browser events and Vue's reactivity cycle:

  • When you select a new option, the browser fires the native change event immediately.
  • Vue updates the v-model bound data property (filter) asynchronously, after the current event loop tick finishes. That means when your changeFilter method runs right away, Vue hasn't had a chance to sync the new value to this.filter yet—so you're seeing the old value.
  • jQuery works here because it's directly reading the current value from the DOM element itself, which already reflects the new selection, skipping Vue's pending update.

Fixes That Keep Your Code Vue-Native

You have two clean, jQuery-free solutions to choose from:

1. Grab the Value Directly from the Event Object

Your change event handler receives an event parameter that lets you access the updated value straight from the DOM element:

methods: {
  changeFilter(event) {
    const latestValue = event.target.value;
    console.log(latestValue); // This gives you the new selection immediately
    
    // Note: v-model will automatically update this.filter shortly after,
    // so you don't need to manually assign it unless you want to override behavior
  }
}

2. Wait for Vue's Update with $nextTick

If you specifically need to use the synced this.filter value, use Vue's $nextTick method to wait until Vue finishes updating the DOM and syncing data:

methods: {
  changeFilter() {
    this.$nextTick(() => {
      console.log(this.filter); // Now this shows the latest selected value
    });
  }
}

$nextTick queues your callback to run after Vue's batch update cycle completes, ensuring your data property is up-to-date.

A Quick Recap

Vue batches DOM updates for performance, which creates this slight delay between the native event firing and the data property updating. You never need to resort to jQuery here—using either the event target or $nextTick keeps your code aligned with Vue's reactivity system, which is way more intuitive once you understand the timing!

内容的提问来源于stack exchange,提问作者Kylie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:12:49