我的数据对象:
data: { selected: { 'type': null,'instrument': null },
我的模板:
<select v-model="selected['instrument']" @change="switchFilter('instrument',$event)"> <option v-for="instrument in instruments" :value="instrument.value">@{{ instrument.text }}</option> </select> <select v-model="selected['type']" @change="switchFilter('type',$event)"> <option v-for="type in types" :value="type.value">@{{ type.text }}</option> </select>
如何同时查看两个选定的索引?每当任何索引更新时,我想做类似的事情:
watch: { selected: function(o,n) { ... } }
解决方法
您可以使用
watcher从vue提供的深度选项.如文档中所述:
To also detect nested value changes inside Objects,you need to pass in deep: true in the options argument. Note that you don’t need to do so to listen for Array mutations.
您的代码如下所示:
watch: { 'selected': { handler: function (val,oldVal) { console.log('watch 1','newval: ',val,' oldVal:',oldVal) },deep: true } }