在Vue3中 watch只能监视四中数据
1.ref定义的数据
2.reactive定义的数据
3.函数返回的一个值
4.包含上述内容的数组
监视ref定义的基本数据类型
监视ref基本类型数据比较简单,下面是一个使用案例:
<template>
<h1>情况一:监视ref定义的数据</h1>
<h2>count:{{ count }}</h2>
<button @click="changeCount">点击count+1</button>
</template>
<script lang="ts" setup name="Person">
import {ref, watch} from "vue";
let count = ref(0);
const changeCount = () => {
count.value += 1;
}
//监视ref定义的数据
const stopWatch = watch(count, (newValue, oldValue) => {
console.log("count改变了", newValue, oldValue)
if (count.value >= 5) {
//停止监视
stopWatch();
}
})
</script>
<style scoped>
</style>
监视ref定义的对象类型数据
数据:直接写数据名,监视的是对象的地址,如果要监视对象的属性,启用深度监视
注意:
deep: true 深度监视 监视对象下所有的属性
参数1:被监视的数据
参数2:监视的回调函数
参数3,配置对象(deep.immediate.....)
<template>
<h1>情况二:监视ref定义的对象类型数据</h1>
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<button @click="changeName">修改姓名</button>
<button @click="changeAge">修改年龄</button>
<button @click="changePerson">修改对象</button>
</template>
<script lang="ts" setup name="Person">
import {ref, watch} from "vue";
let person = ref({name: "张三", age: 25});
const changeName = () => {
person.value.name += "~";
}
const changeAge = () => {
person.value.age += 1;
}
const changePerson = () => {
person.value = {name: "李四", age: 30};
}
//监视ref定义的对象类型数据
//监视person对象的整体改变
// watch(person, (newValue, oldValue) => {
// console.log("person改变了", newValue, oldValue)
// })
watch(person, (newValue, oldValue) => {
console.log("person改变了", newValue, oldValue)
}, {deep: true})
</script>
<style scoped>
</style>
监视reactive定义的对象类型数据
注意:默认开启深度监视
<template>
<h1>情况二:监视ref定义的对象类型数据</h1>
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<button @click="changeName">修改姓名</button>
<button @click="changeAge">修改年龄</button>
<button @click="changePerson">修改对象</button>
<hr/>
<h2>测试:{{ obj.a.b.c.d }}</h2>
<button @click="test">修改d</button>
</template>
<script lang="ts" setup name="Person">
import {reactive, watch} from "vue";
let person = reactive({name: "张三", age: 25});
let obj = reactive({a: {b: {c: {d: 666}}}})
const changeName = () => {
person.name += "~";
}
const changeAge = () => {
person.age += 1;
}
const changePerson = () => {
// person = {name: "李四", age: 30};
Object.assign(person, {name: "李四", age: 30})
}
const test = () => {
obj.a.b.c.d = 888;
}
/**
* 通过 reactive 定义的对象类型数据 监视 默认开启深度监视
*/
watch(person, (newValue, oldValue) => {
console.log("person改变了", newValue, oldValue)
})
watch(obj, (newValue, oldValue) => {
console.log("obj改变了", newValue, oldValue)
})
</script>
<style scoped>
</style>
监视ref或者reactive定义的对象类型数据中的 “某个属性”
<template>
<div>
<h2>监视reactive或者 ref定义的对象类型的某个属性</h2>
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<h2>汽车:{{ person.car.c1 }}、{{ person.car.c2 }}</h2>
<button @click="changeName">修改姓名</button>
<button @click="changeAge">修改年龄</button>
<button @click="changeC1">修改第一台车</button>
<button @click="changeC2">修改第二台车</button>
<button @click="changeCar">修改整辆车</button>
</div>
</template>
<script lang="ts" setup name="Person">
import {reactive, watch} from "vue";
let person = reactive({
name: "张三",
age: 25,
car: {
c1: '比亚迪',
c2: "雅迪"
}
})
const changeName = () => {
person.name += "!";
}
const changeAge = () => {
person.age += 1;
}
const changeC1 = () => {
person.car.c1 = "奥迪";
}
const changeC2 = () => {
person.car.c2 = "小牛";
}
const changeCar = () => {
person.car = {c1: "爱玛", c2: '小刀'};
}
watch(() => person.car, (newValue, oldValue) => {
console.log("car发生了改变", newValue, oldValue);
}, {deep: true})
//直接写的方式监视 只能监视到 属性的改变,并且在替换后 不能在监视
watch(person.car, (newValue, oldValue) => {
console.log("car发生了改变", newValue, oldValue);
}, {deep: true})
</script>
<style scoped>
</style>
监视上述多个数据
<template>
<div>
<h2>监视上述多个数据</h2>
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<h2>汽车:{{ person.car.c1 }}、{{ person.car.c2 }}</h2>
<button @click="changeName">修改姓名</button>
<button @click="changeAge">修改年龄</button>
<button @click="changeC1">修改第一台车</button>
<button @click="changeC2">修改第二台车</button>
<button @click="changeCar">修改整辆车</button>
</div>
</template>
<script lang="ts" setup name="Person">
import {reactive, watch} from "vue";
let person = reactive({
name: "张三",
age: 25,
car: {
c1: '比亚迪',
c2: "雅迪"
}
})
const changeName = () => {
person.name += "!";
}
const changeAge = () => {
person.age += 1;
}
const changeC1 = () => {
person.car.c1 = "奥迪";
}
const changeC2 = () => {
person.car.c2 = "小牛";
}
const changeCar = () => {
person.car = {c1: "爱玛", c2: '小刀'};
}
watch([() => person.name, person.car], (newValue, oldValue) => {
console.log("person.name, person.car",newValue, oldValue)
}, {deep: true});
</script>
<style scoped>
</style>