Vue3中watch使用以及新旧值相同问题

ref()

基本数据类型

直接监听

const refData = ref(null)
watch(refData, (newValue, oldValue) => {
  console.log(newValue, oldValue)
})

引用数据类型 

  • 需要deep

需添加 deep:true 才能监听值改变,但newValue与 oldValue值相同

const refData = ref([1, 2, 3])
watch(
  refData,
  (newValue, oldValue) => {
    console.log(newValue, oldValue)
  },
  { deep: true }
)
//newValue:Proxy(Array) {0: 1, 1: 2, 2: 3, 3: 4}
//oldValue:Proxy(Array) {0: 1, 1: 2, 2: 3, 3: 4}


//修改refData
const changeData = () => {
  refData.value.push(4)
}
  • 深拷贝并写作getter形式

通过深拷贝(以扩展运算符为例),解决newValue与 oldValue值相同问题

const refData = ref([1, 2, 3])
watch(
  () => [...refData.value],
  (newValue, oldValue) => {
    console.log(newValue, oldValue)
  },
  { deep: true }
)
//newValue:[1, 2, 3, 4]
//oldValue:[1, 2, 3]


//修改refData
const changeData = () => {
  refData.value.push(4)
}

reactive()

  • 无需加deep,默认deep:true

能监听值改变,但newValue与 oldValue值相同

const reactiveData = reactive({ content: 'reactiveContent' })
watch(reactiveData, (newValue, oldValue) => {
  console.log(newValue, oldValue)
})
//newValue:Proxy(Object) {content: 'reactiveContentChange'}
//oldValue:Proxy(Object) {content: 'reactiveContentChange'}

//修改reactiveData
const changeData = () => {
  reactiveData.content = 'reactiveContentChange'
}
  • 解决值相同问题

Object:直接监听具体值而非整个对象,并写作getter形式 

const reactiveData = reactive({ content: 'reactiveContent' })
watch(
  () => reactiveData.content,
  (newValue, oldValue) => {
    console.log(newValue, oldValue)
  }
)
//newValue:reactiveContentChange 
//oldValue:reactiveContent

//修改reactiveData
const changeData = () => {
  reactiveData.content = 'reactiveContentChange'
}

Array:深拷贝,并写作getter形式 

const reactiveData = reactive([1, 2, 3])
watch(
  () => [...reactiveData],
  (newValue, oldValue) => {
    console.log( newValue, oldValue)
  }
)
//newValue:[1, 2, 3, 4]
//oldValue:[1, 2, 3]


//修改refData
const changeData = () => {
  reactiveData.push(4)
}




注:扩展运算符只对一层的数组或对象是深拷贝,多层的不要用这深拷贝!!!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3watch使用方式有所改变Vue 3引入了一个新的API即`watchEffect`,它可以用来监听响应式数据的变化并执行相应的操作。 `Effect`函数接受一个回调函数作为参数,该调函数会在响应式数据发生化时被调用。在回调函数,你可以执行任何需要的操作,比如更新DOM、发送网络请求等。 下面是一个使用`Effect`的示例: ```javascript import reactive, watchEffect } from 'vue'; const state = reactive({ count: 0, }); watchEffect(() => { console.log('count changed:', state.count); }); // 修改count的 state.count++; // 输出:count changed: 1 state.count++; // 输出:count changed: 2 ``` 在上面的示例,我们创建了一个响应式对象`state`,其包含一个属性`count`。然后,我们使用`watchEffect`来监听`state.count`的变化,并在每次变化时打印出新的。 除了`watchEffect`,Vue 3还提供了`watch`函数,它可以用来监听指定的响应式数据或计算属性的变化。与Vue 2的`$watch`方法类似,你可以通过传递一个回调函数和可选的配置选项来使用它。 下面是一个使用`watch`的示例: ```javascript import { reactive, watch } from 'vue'; const state = reactive({ count: 0, }); watch( () => state.count, (newValue, oldValue) => { console.log('count changed:', newValue); } ); // 修改count的 state.count++; // 输出:count changed: 1 state.count++; // 输出:count changed: 2 ``` 在上面的示例,我们使用`watch`来监听`state.count`的变化,并在每次变化时打印出新的。回调函数的第一个参数是新的,第二个参数是旧的。 总结一下,Vue 3watch使用方式有两种:`watchEffect`和`watch`。`watchEffect`用于监听响应式数据的变化,而`watch`用于监听指定的响应式数据或计算属性的变化。 --问题--: 1. Vue 3watch有哪些使用方式? 2 如何使用watchEffect来监听响应式数据的变化? 3. 如何使用watch来监听指定的响应式数据或计算属性的变化?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值