My data object:
data: {
selected: {
'type': null,
'instrument': null
},
My template:
<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>
How can I watch both selected indexes at the same time? I want to do something like this everytime any of the indexes updates:
watch: {
selected: function(o, n) {
...
}
}
I think you can do this:
If you are just trying to calculate a new data from
selected
, you can just use computed properties, since the data of Vue are reactive, the computed properties can also detect the changes of data.If you want to use a single function to watch the entire object, you can use
$watch
withdeep: true
:note that
'$data.selected'
is a string, Vue will parse it.and in your methods:
You can use
deep
option provided by the watcher from vue. As stated in the docs:You code will look like following: