上篇文章: 【Vue】Vue3.0 (十二)、watch对ref定义的基本类型、对象类型;reactive定义的对象类型的监视使用
作者主页:点击!
Vue专栏:点击!
⏰️创作时间:2024年10月17日21点56分
watchEffect
和watch
都是 Vue.js
中用于响应式数据变化的特性,但它们之间存在一些区别,主要体现在以下几个方面:
watch('somedata', (newval, oldval) => {
// 做一些事情
});
- 也可以监听多个数据的变化,多个数据的时候需要区分是监听对象还是基本的数据类型,在写法上有些区别,具体可见我上一篇文章:[【Vue】Vue3.0 (十二)、watch对ref定义的基本类型、对象类型;reactive定义的对象类型的监视使用](https://blog.csdn.net/qq_39666711/article/details/142974884?spm=1001.2014.3001.5502)
例如:
watch(['data1', 'data2'], (newValue,oldValue) => {
// 做一些事情
// 比如输出一些内容:
console.log('数据中data1或者data2变化了',newValue,oldValue)
});
watcheffect(() => {
// 依赖于响应式数据的操作
});
immediate: true
,则 watch 在页面首次加载时就会执行。deep: true
)才能捕获到变化。watch('somedata', (newval, oldval) => {
console.log('旧值:', oldval);
console.log('新值:', newval);
});
- watch:
- 适用于需要细粒度控制响应式数据变化的场景,比如需要手动停止监听或访问数据变化前后的值。
- 例如,在一个表单组件中,监听表单字段的变化,以便在用户输入时进行实时验证。
- watchEffect:
- 更适合用于自动追踪响应式数据并触发副作用的场景,如执行异步操作或更新 UI。
- 例如,在一个数据可视化组件中,根据数据的变化自动更新图表。
import { ref, watch } from 'vue';
export default {
setup() {
const count = ref(0);
const name = ref('John');
// 监听单个数据
watch(count, (newCount, oldCount) => {
console.log('count 的值从', oldCount, '变为了', newCount);
});
// 监听多个数据
watch([count, name], ([newCount, newName], [oldCount, oldName]) => {
console.log('count 的值从', oldCount, '变为了', newCount);
console.log('name 的值从', oldName, '变为了', newName);
});
return {
count,
name
};
}
};
import { ref, watchEffect } from 'vue';
export default {
setup() {
const count = ref(0);
const name = ref('John');
watchEffect(() => {
console.log('count 的值为:', count.value);
console.log('name 的值为:', name.value);
});
return {
count,
name
};
}
};
<template>
<div class="person">
<h2>情况4:watch监控ref定义的对象类型数据h2>
<h3>姓名:{{person.name}} h3>
<h3>年龄:{{person.age}} h3>
<h3>汽车:{{person.car.c1}} /{{person.car.c2}} h3>
<button @click="changeName">修改姓名button>
<button @click="changeAge">修改年龄button>
<button @click="changeC1">修改c1button>
<button @click="changeC2">修改c2button>
<button @click="changeCar">修改Carbutton>
div>
template>
<script lang="ts" setup name="Person">
import { reactive, ref, computed, watch } from 'vue'
//数据
let person=reactive({
name:'张三',
age:18,
car:{
c1:'宝马',
c2:'奔驰'
}
})
function changeName(){
person.name+='~';
}
function changeAge(){
person.age+=1;
}
function changeC1(){
person.car.c1='大众1'
}
function changeC2(){
person.car.c2='大众2'
}
function changeCar(){
person.car={c1:'雅迪',c2:'艾玛'}
}
//情况4、watch监控reactive定义的对象类型数据中的某个属性,且该属性是一个基本类型的数据,则该属性需要写成一个有返回值的函数
// watch(()=>{return person.name},(newVal,oldVal)=>{
// console.log('person.name变化了',newVal,oldVal);
// })
// watch([()=>person.name,person.car],(newValue,oldValue)=>{
// console.log('person.car变化了',newValue,oldValue)
// },{deep:true})
watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
console.log('person.car变化了',newValue,oldValue)
},{deep:true})
script>
<style>
.person {
background-color: skyblue;
box-shadow: 0 0 10px;
border-radius: 10px;
padding: 20px;
}
li {
font: 1em sans-serif;
}
style>
验证结果:
点击修改 name和名字都会引起watch的监听;
都能监听响应式数据的变化,不同的是监听数据变化的方式不同
watch
:要明确指出监视的数据
watchEffect
:不用明确指出监视的数据(函数中用到哪些属性,那就监视哪些属性)。
watch
和watchEffect
都是非常有用的响应式数据监听工具,在实际使用中,需要根据具体的需求和场景来选择合适的方法。如果需要对数据变化进行更精细的控制,或者需要获取数据变化前后的值,那么
watch
可能更适合;如果只是需要在数据变化时自动执行一些副作用,那么watchEffect
可能更简洁方便。