Vue: watch only fired once

Created on 7 Feb 2016  ·  6Comments  ·  Source: vuejs/vue

I have the following scenario:

<template v-for="field in fields">
    <input type="text" v-model="values[field.key]" />
</template>

with the following code:

var vue = new Vue({
    el: "#vue",
    data: {
        fields: [{key:"a"},{key:"b"}],
        values: {}
    },
    watch: {
        values: function (val, old) {
            alert("change!");
        }
    }
});

The problem is that the alert only occures the first time each input field is modified. As far as I can tell this only seems to occur when the two-way binding target uses a variable.

By inspecting the vue data I can see that the values keep updating even though the watch function isn't invoked.

I'm using the method proposed by @yyx990803 in issue #1056.

you can already do that with v-model="form[field.name]".

Most helpful comment

You need a deep watcher.

All 6 comments

You need a deep watcher.

I have exactly the same scenario with the same problem, and still not working with deep: true:

var vue = new Vue({
    el: "#vue",
    data: {
        fields: [{key:"a"},{key:"b"}],
        values: {}
    },
    watch: {
        values: function (val, old) {
            alert("change!");
        },
        deep: true
    }
});

Have you solved the problem @pqvst?

@maximelebreton Your usage of deep is incorrect

watch: {
  values: {
    handler: function (val, old) { ... },
    deep: true
  }
}

https://vuejs.org/v2/api/#watch

oups, sorry,
I wanted to make quick, and my example is wrong...

I've made a jsfiddle to be more effective: https://jsfiddle.net/dq90hkb8/
but I ultimately realize that the problem is different from your...

Sorry for my mistake, I'll open a new issue.

@maximelebreton I'm having this exact issue. Did you figure out what the problem was?

The issue is the way the array is being mutated. See https://vuejs.org/v2/guide/list.html#Array-Change-Detection

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guan6 picture guan6  ·  3Comments

wufeng87 picture wufeng87  ·  3Comments

hiendv picture hiendv  ·  3Comments

franciscolourenco picture franciscolourenco  ·  3Comments

bfis picture bfis  ·  3Comments