<!DOCTYPE html>
<html>
<div id="app">
a
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const app = Vue.createApp({
data() {
return {
a: 1,
b: 2,
c: {
d: 3,
e: 4
}
}
},
created() {
this.$watch('a', (newVal, oldVal) => {
})
this.$watch(
() => this.a + this.b,
(newVal, oldVal) => {
}
)
}
}).mount('#app')
</script>
</html>
加上了。
<!DOCTYPE html>
<html>
<div id="app">
<div>
<input type="number" v-model="rr">
{{a}}
<input type="number" v-model="b">
{{b}}
<input type="number" v-model="c">
{{c.d}}
<input type="number" v-model="d">
{{c.e}}
{{a}}
b
{{ b }}
</div>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const app = Vue.createApp({
data() {
return {
a: 1,
b: 2,
c: {
d: 3,
e: 4
}
}
},
created() {
this.$watch('a', (newVal, oldVal) => {
})
this.$watch(
() => this.a + this.b,
(newVal, oldVal) => {
}
)
}
}).mount('#app')
</script>
</html>
但是,$watch这个有什么用?