Watch multiple values in the Options API. #8770
Replies: 4 comments
|
Hey, there's no any official way in documentation but you can do something like this. |
|
watchEffect |
|
Personally, I chose something like: <template>
<input v-model="name" />
<input v-model="surname" />
</template>
<script>
export default {
data() {
return {
name: 'jon',
surname: 'snow',
}
},
computed: {
fullName() {
return this.name + ' ' + this.surname;
}
},
watch: {
fullName(newValue, oldValue) {
//
}
}
}
</script> |
|
It's mostly a syntax/API shape thing, not a reactivity restriction. In the Options API the The two workarounds that people use in the Options API are both fine:
computed: {
watchedValues() {
return [this.name, this.surname];
}
},
watch: {
watchedValues(newVal, oldVal) {
this.fullName = newVal.join(' ');
}
}
One thing to watch out for with option 1: the handler fires whenever any element of the array changes, and you get the whole new and old arrays, so if you need to know which value changed you have to compare them yourself. If you don't need the old values at all, — Lazar Bukejlovic, founder: https://izracunpostotka.com/ |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Why can't multiple values be watched by the same handler in the options API? Is it that different from the the Composition API or is it a syntax issue? Or maybe there is a way to do it that isn't in the docs?
Edit: I'm wondering specifically WHY? Are there restriction or has it just not been looked at yet?
All reactions