Vue3技术4

  • watch监视属性
    • watch监视ref定义的数据
      • 情况一:监视ref所定义的一个响应式数据
        • App.vue
        • Demo.vue
      • 情况二:监视ref所定义的多个响应式数据
        • App.vue
        • Demo.vue
      • 添加immediate属性
        • Demo.vue
    • watch监视reactive定义的数据
      • 情况一:监视reactive所定义的一个响应式数据的全部属性
        • Demo.vue
      • 情况二:监视reactive所定义的一个响应式数据的某个属性
        • Demo.vue
      • 情况三:监视reactive所定义的一个响应式数据的某些属性
        • Demo.vue
      • 特殊情况
        • Demo.vue
    • 总结
    • watch时value的问题
      • 使用ref生成对象
        • Demo.vue
        • Demo.vue

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>

Vue3技术4之watch监视属性、watch时value问题相关推荐

  1. Vue 监视属性 watch

    <!--监视属性 watch:1. 当被监视的属性变化时,回调函数自动调用,进行相关操作2. 监视的属性必须存在,才能进行监视3. 监视的两种写法:(1). new Vue 时传入 watch ...

  2. Vue学习之监视属性watch

    简介 设置监视属性的作用: 当被监视的属性发生变化时,回调函数会自动调用,进行相关操作. 监视的熟悉必须存在,才能进行监视. 使用vue的配置对象中的watch属性进行配置. <!DOCTYPE ...

  3. 11.监视属性——watch

    一.监视属性watch 1.当被监视的属性变化时,回调函数自动调用,进行相关操作 2.监视的属性必须存在,才能进行监视 3.监视的两种写法 (1)new Vue时传入watch配置 (2)通过vm.$ ...

  4. 【vue系列-03】vue的计算属性,列表,监视属性及原理

    vue的核心属性 一,vue核心属性 1,计算属性 2,监视属性 3,样式绑定 3.1,class样式绑定 3.2,style样式绑定 4,条件渲染 5,列表渲染 5.1,遍历列表 5.2,key的作 ...

  5. Vue计算属性、监视属性

    一.Vue计算属性 1. 定义:要用的属性不存在,要通过已有属性计算得来 2. 原理:底层借助了Object.defineproperty方法提供的getter和setter 3. get函数什么时候 ...

  6. vue计算属性与监视属性

    计算属性与监视属性 计算属性 在computed对象中定义计算属性的方法,在页面中使用{{方法名}} 监视属性 通过 vm对象中的$watch方法或者 watch配置来监视指定的属性, 当属性发生变化 ...

  7. Vue9.2天气案例_监视属性

    <!DOCTYPE html>> <html><head><title>天气案例_监视属性</title><meta chars ...

  8. 基于springboot+uniapp+vue3技术栈开发的开源跨平台小程序

    塔可商城, 一个基于springboot+uniapp+vue3技术栈开发的开源跨平台小程序.管理后台,后端服务的项目,它内置提供了会员分销, 区域代理, 商品零售等功能的新零售电商系统.强大弹性的架 ...

  9. ajax最核心的技术,Ajax技术的核心以及方法属性

    这次给大家带来Ajax技术的核心以及方法属性,使用Ajax技术核心以及方法属性的注意事项有哪些,下面就是实战案例,一起来看一下. 一.什么是Ajax Ajax英文全称为" Asynchr J ...

最新文章

  1. JAVA实现 springMVC方式的微信接入、实现消息自动回复
  2. 25 iOS performance TipsTricks 笔记
  3. Docker建网站 4条命令搞定
  4. 【Android 逆向】函数拦截 ( ARM 架构下的插桩拦截 | 完整代码示例 )
  5. 基于订阅的服务通讯架构体系
  6. cp无法获取文件状态stat_Node.js从零开始——文件系统
  7. NVIDIA DIGITS-2.0 + Ubuntu 14.04 + CUDA 7.0 + cuDNN 7.0 + Caffe 0.13.0环境配置
  8. 对于窗口大小为n个滑动窗口,最多可以有( )帧已发送但没有确认。
  9. ****阿里云使用+快速运维总结(不断更新)
  10. Zabbix 使用微信接收报警信息
  11. mysql调用tag标签_DEDECMS5.5/5.6/5.7列表页调用TAG标签(热门标签)的两种方法
  12. 摄影测量学(第三版)_王佩军_考试复习资料
  13. 文件服务器资源管理器无法在加载wmI对象,无法通过WMI
  14. OCUI界面设计:导航控制器
  15. 捋一捋Android的转场动画
  16. php开源 饭馆记账软件_个人记账软件 - 开源免费
  17. 手机打字软件如何测试打字快,手机打字学好拼音最笨的方法 手机打字怎么练才能快...
  18. linux压缩文件命令_24.gzip、unzip命令详解 - 钟桂耀
  19. 当游戏直播遇上网课教育:虎牙、斗鱼的跨界梦圆的了吗?
  20. Qt的connect函数和disconnect函数

热门文章

  1. 2021Unity教程:Unity官方中文版免费下载方法(黑皮肤可选)无需破解!
  2. 《神奇的数学》读后感_《走进奇妙的数学世界》读后感
  3. 万豪国际集团亚太第800家酒店正式开业
  4. Java虚拟机(一)结构原理与运行时数据区域
  5. Vivo手机获取文件管理器里的文件路径为空
  6. ESD门禁管理系统方案
  7. vue + echarts 之饼形图
  8. myQNX account试用申请流程(license申请)
  9. find vba 模糊_EXCEL——VBA实现模糊查找并获取查找到的单元格内容
  10. 关于学校edu邮箱登录第三方邮件客户端(例如Outlook、Foxmail、QQ邮箱、邮件App等)的注意事项