Vue3技术4之watch监视属性、watch时value问题

watch监视属性

watch监视ref定义的数据

情况一:监视ref所定义的一个响应式数据

App.vue
<template>
  <Demo></Demo>
</template>

<script>
import Demo from "@/components/Demo";
export default {
  name: 'App',
  components: {Demo},
}
</script>

Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
</template>

<script>
import {ref,watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)

      //情况一:监视ref所定义的一个响应式数据
      watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      })

      //返回一个对象(常用)
      return{
          sum
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

情况二:监视ref所定义的多个响应式数据

App.vue
<template>
  <Demo></Demo>
</template>

<script>
import Demo from "@/components/Demo";
export default {
  name: 'App',
  components: {Demo},
}
</script>

Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
</template>

<script>
import {ref,watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')

      //情况一:监视ref所定义的一个响应式数据
      watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      })

      //情况二:监视ref所定义的多个响应式数据
      watch([sum,msg],(newValue,oldValue) => {
        console.log("sum或msg发生改变了",newValue,oldValue)
      })


      //返回一个对象(常用)
      return{
          sum,
          msg
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

添加immediate属性

Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
</template>

<script>
import {ref,watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')

      //情况一:监视ref所定义的一个响应式数据
      /*watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      },{immediate:true})*/

      //情况二:监视ref所定义的多个响应式数据
      watch([sum,msg],(newValue,oldValue) => {
        console.log("sum或msg发生改变了",newValue,oldValue)
      },{immediate:true})


      //返回一个对象(常用)
      return{
          sum,
          msg
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

watch监视reactive定义的数据

情况一:监视reactive所定义的一个响应式数据的全部属性

  1. 注意:此处无法正确获取oldValue值
  2. 注意:强制开启深度监视(deep配置无效)
Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
  <hr>
  <h2>个人信息</h2>
  <h2>姓名:{{person.name}}</h2>
  <h2>年龄:{{person.age}}</h2>
  <h2>薪资:{{person.job.j1.salary}}K</h2>
  <button @click="person.name+='~'">修改信息</button>
  <button @click="person.age++">增加年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import {reactive, ref, watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')
      let person=reactive({
        name:'张三',
        age:18,
        job:{
          j1:{
            salary:20
          }
        }
      })

      //情况一:监视ref所定义的一个响应式数据
      /*watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      },{immediate:true})*/

      //情况二:监视ref所定义的多个响应式数据
      // watch([sum,msg],(newValue,oldValue) => {
      //   console.log("sum或msg发生改变了",newValue,oldValue)
      // },{immediate:true})

      /*
        情况三:监视reactive所定义的一个响应式数据的全部属性
        1.注意:此处无法正确获取oldValue值
        2.注意:强制开启深度监视(deep配置无效)
      */
      watch(person,(newValue,oldValue) => {
        console.log("person值改变了",newValue,oldValue)
      },{deep:false})  //此处的deep配置无效



      //返回一个对象(常用)
      return{
          sum,
          msg,
          person
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

情况二:监视reactive所定义的一个响应式数据的某个属性

Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
  <hr>
  <h2>个人信息</h2>
  <h2>姓名:{{person.name}}</h2>
  <h2>年龄:{{person.age}}</h2>
  <h2>薪资:{{person.job.j1.salary}}K</h2>
  <button @click="person.name+='~'">修改信息</button>
  <button @click="person.age++">增加年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import {reactive, ref, watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')
      let person=reactive({
        name:'张三',
        age:18,
        job:{
          j1:{
            salary:20
          }
        }
      })

      //情况一:监视ref所定义的一个响应式数据
      /*watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      },{immediate:true})*/

      //情况二:监视ref所定义的多个响应式数据
      // watch([sum,msg],(newValue,oldValue) => {
      //   console.log("sum或msg发生改变了",newValue,oldValue)
      // },{immediate:true})

      /*
        情况三:监视reactive所定义的一个响应式数据的全部属性
        1.注意:此处无法正确获取oldValue值
        2.注意:强制开启深度监视(deep配置无效)
      */
      /*watch(person,(newValue,oldValue) => {
        console.log("person值改变了",newValue,oldValue)
      },{deep:false})  //此处的deep配置无效*/

      //情况四:监视reactive所定义的一个响应式数据的某个属性
      watch(()=>person.age,(newValue,oldValue) => {
        console.log("person的age属性改变了",newValue,oldValue)
      })



      //返回一个对象(常用)
      return{
          sum,
          msg,
          person
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

情况三:监视reactive所定义的一个响应式数据的某些属性

Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
  <hr>
  <h2>个人信息</h2>
  <h2>姓名:{{person.name}}</h2>
  <h2>年龄:{{person.age}}</h2>
  <h2>薪资:{{person.job.j1.salary}}K</h2>
  <button @click="person.name+='~'">修改信息</button>
  <button @click="person.age++">增加年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import {reactive, ref, watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')
      let person=reactive({
        name:'张三',
        age:18,
        job:{
          j1:{
            salary:20
          }
        }
      })

      //情况一:监视ref所定义的一个响应式数据
      /*watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      },{immediate:true})*/

      //情况二:监视ref所定义的多个响应式数据
      // watch([sum,msg],(newValue,oldValue) => {
      //   console.log("sum或msg发生改变了",newValue,oldValue)
      // },{immediate:true})

      /*
        情况三:监视reactive所定义的一个响应式数据的全部属性
        1.注意:此处无法正确获取oldValue值
        2.注意:强制开启深度监视(deep配置无效)
      */
      /*watch(person,(newValue,oldValue) => {
        console.log("person值改变了",newValue,oldValue)
      },{deep:false})  //此处的deep配置无效*/

      //情况四:监视reactive所定义的一个响应式数据的某个属性
      /*watch(()=>person.age,(newValue,oldValue) => {
        console.log("person的age属性改变了",newValue,oldValue)
      })*/

      //情况五:监视reactive所定义的一个响应式数据的某些属性
      watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
        console.log("person的name或age属性改变了",newValue,oldValue)
      })


      //返回一个对象(常用)
      return{
          sum,
          msg,
          person
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

特殊情况

Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
  <hr>
  <h2>个人信息</h2>
  <h2>姓名:{{person.name}}</h2>
  <h2>年龄:{{person.age}}</h2>
  <h2>薪资:{{person.job.j1.salary}}K</h2>
  <button @click="person.name+='~'">修改信息</button>
  <button @click="person.age++">增加年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import {reactive, ref, watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')
      let person=reactive({
        name:'张三',
        age:18,
        job:{
          j1:{
            salary:20
          }
        }
      })

      //情况一:监视ref所定义的一个响应式数据
      /*watch(sum,(newValue,oldValue) => {
        console.log("sum改变了",newValue,oldValue)
      },{immediate:true})*/

      //情况二:监视ref所定义的多个响应式数据
      // watch([sum,msg],(newValue,oldValue) => {
      //   console.log("sum或msg发生改变了",newValue,oldValue)
      // },{immediate:true})

      /*
        情况三:监视reactive所定义的一个响应式数据的全部属性
        1.注意:此处无法正确获取oldValue值
        2.注意:强制开启深度监视(deep配置无效)
      */
      /*watch(person,(newValue,oldValue) => {
        console.log("person值改变了",newValue,oldValue)
      },{deep:false})  //此处的deep配置无效*/

      //情况四:监视reactive所定义的一个响应式数据的某个属性
      /*watch(()=>person.age,(newValue,oldValue) => {
        console.log("person的age属性改变了",newValue,oldValue)
      })*/

      //情况五:监视reactive所定义的一个响应式数据的某些属性
      /*watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
        console.log("person的name或age属性改变了",newValue,oldValue)
      })*/

      //特殊情况
      watch(()=>person.job,(newValue,oldValue) => {
        console.log("person的job变化了",newValue,oldValue)
      },{deep:true})  //此处由于是监视reactive所定义的对象中的某个属性,所以deep配置有效

      //返回一个对象(常用)
      return{
          sum,
          msg,
          person
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

总结

  1. 与Vue2.x中watch配置功能一致
  2. 两个小“坑”:
    (1)监视reactive定义的响应式数据时:oldValue无法正确获取,强制开启了深度监视(deep配置失效)
    (2)监视reactive定义的响应式数据中某个属性时:deep配置有效
//情况一:监视ref所定义的一个响应式数据
watch(sum,(newValue,oldValue) => {
  console.log("sum改变了",newValue,oldValue)
},{immediate:true})

//情况二:监视ref所定义的多个响应式数据
watch([sum,msg],(newValue,oldValue) => {
  console.log("sum或msg发生改变了",newValue,oldValue)
},{immediate:true})

/*
  情况三:监视reactive所定义的一个响应式数据的全部属性
  1.注意:此处无法正确获取oldValue值
  2.注意:强制开启深度监视(deep配置无效)
*/
watch(person,(newValue,oldValue) => {
  console.log("person值改变了",newValue,oldValue)
},{deep:false})  //此处的deep配置无效

//情况四:监视reactive所定义的一个响应式数据的某个属性
watch(()=>person.age,(newValue,oldValue) => {
  console.log("person的age属性改变了",newValue,oldValue)
})

//情况五:监视reactive所定义的一个响应式数据的某些属性
watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
  console.log("person的name或age属性改变了",newValue,oldValue)
})

//特殊情况
watch(()=>person.job,(newValue,oldValue) => {
  console.log("person的job变化了",newValue,oldValue)
},{deep:true}) //此处由于是监视reactive所定义的对象中的某个属性,所以deep配置有效

watch时value的问题

使用ref生成对象

  1. 方法一:使用deep深度监测
Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
  <hr>
  <h2>个人信息</h2>
  <h2>姓名:{{person.name}}</h2>
  <h2>年龄:{{person.age}}</h2>
  <h2>薪资:{{person.job.j1.salary}}K</h2>
  <button @click="person.name+='~'">修改信息</button>
  <button @click="person.age++">增加年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import {ref, watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')
      let person=ref({
        name:'张三',
        age:18,
        job:{
          j1:{
            salary:20
          }
        }
      })

      watch(sum,(newValue,oldValue)=>{
        console.log("sum值发生了改变",newValue,oldValue)
      })

      //对于ref生成的对象类型数据检测,使用deep,或者直接person.value进行监测
      watch(person,(newValue,oldValue)=>{
        console.log("person值发生了改变",newValue,oldValue)
      },{deep:true})

      



      //返回一个对象(常用)
      return{
          sum,
          msg,
          person
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

  1. 方法二:使用person.value解决
Demo.vue
<template>
  <h2>和为:{{sum}}</h2>
  <button @click="sum++">和+1</button>
  <hr>
  <h2>当前的信息为:{{msg}}</h2>
  <button @click="msg+=''">修改信息</button>
  <hr>
  <h2>个人信息</h2>
  <h2>姓名:{{person.name}}</h2>
  <h2>年龄:{{person.age}}</h2>
  <h2>薪资:{{person.job.j1.salary}}K</h2>
  <button @click="person.name+='~'">修改信息</button>
  <button @click="person.age++">增加年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import {ref, watch} from 'vue'
export default {
    name: "Demo",
    setup(){
      //数据
      let sum=ref(0)
      let msg=ref('你好啊')
      let person=ref({
        name:'张三',
        age:18,
        job:{
          j1:{
            salary:20
          }
        }
      })

      watch(sum,(newValue,oldValue)=>{
        console.log("sum值发生了改变",newValue,oldValue)
      })

      //对于ref生成的对象类型数据检测,使用deep,或者直接person.value进行监测
      /*watch(person,(newValue,oldValue)=>{
        console.log("person值发生了改变",newValue,oldValue)
      },{deep:true})*/


      watch(person.value,(newValue,oldValue)=>{
        console.log("person值发生改变啦~~~~",newValue,oldValue)
      })




      //返回一个对象(常用)
      return{
          sum,
          msg,
          person
      }
    },
}
</script>

<style scoped>

</style>

请添加图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值