A watcher is a method that watches a data property with the same name.
A watcher runs every time the data property value changes.
Use a watcher if a certain data property value requires an action.
Watchers is the fourth configuration option in the Vue instance that we will learn. The first three configuration options we have already looked at are 'data', 'methods' and 'computed'.
As with 'data', 'methods' and 'computed' watchers also has a reserved name in the Vue instance: 'watch'.
const app = Vue.createApp({
data() {
...
},
watch: {
...
},
computed: {
...
},
methods: {
...
}
})
As mentioned in the green area at the top, a watcher monitors a data property with the same name.
We never call a watcher method. It is only called automatically when the property value changes.
The new property value is always available as an input argument to the watcher method, and so is the old value.
An <input type="range">
element is used to change a value 'rangeVal'. A watcher is used to prevent the user from choosing values between 20 and 60 that are considered illegal.
<input type="range" v-model="rangeVal">
<p>{{ rangeVal }}</p>
const app = Vue.createApp({
data() {
rangeVal: 70
},
watch: {
rangeVal(val){
if( val>20 && val<60) {
if(val<40){
this.rangeVal = 20;
}
else {
this.rangeVal = 60;
}
}
}
}
})
Try it Yourself »
In addition to the new property value, the previous property value is also automatically available as an input argument to watcher methods.
We set up click event on a <div>
element to record mouse pointer x-position 'xPos' with a method 'updatePos'. A watcher calculates the difference in pixels between the new x-position and the previous with the use of old and new input arguments to the watcher method.
<div v-on:click="updatePos"></div>
<p>{{ xDiff }}</p>
const app = Vue.createApp({
data() {
xPos: 0,
xDiff: 0
},
watch: {
xPos(
newVal,oldVal){
this.xDiff = newVal-oldVal
}
},
methods: {
updatePos(evt) {
this.xPos = evt.offsetX
}
}
})
Try it Yourself »
We can also use new and old values to give feedback to the user the exact moment the input goes from being invalid to valid:
The value from an <input>
element is connected to a watcher. If the value includes a '@' it is considered a valid e-mail address. The user gets a feedback text to inform if the input is valid, invalid, or if it just got valid with the last keystroke.
<input v-type="email" v-model="inpAddress">
<p v-bind:class="myClass">{{ feedbackText }}</p>
const app = Vue.createApp({
data() {
inpAddress: '',
feedbackText: '',
myClass: 'invalid'
},
watch: {
inpAddress(newVal,oldVal) {
if( !newVal.includes('@') ) {
this.feedbackText = 'The e-mail address is NOT valid';
this.myClass = 'invalid';
}
else if( !oldVal.includes('@') && newVal.includes('@') ) {
this.feedbackText = 'Perfect! You fixed it!';
this.myClass = 'valid';
}
else {
this.feedbackText = 'The e-mail address is valid :)';
}
}
}
})
Try it Yourself »
Watchers and methods are both written as functions, but there are many differences:
Watchers and computed properties are both written as functions.
Watchers and computed properties are both called automatically when a dependency change, and never called from HTML.
Here are some differences between computed properties and watchers:
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!