vue3基础之watch监听

watch监听基本使用

  • watch是一个函数,需要引入使用,监听多个数据只要多次调用watch函数即可
  • watch第一个参数是监听的数据
  • watch第二个参数是监听后的回调函数,参数为newVal和oldVal
  • watch第三个参数是配置对象,包含immediate和deep

watch监听ref基本类型数据

  • 直接监听即可,不用设置deep深度监听
<template>
  <div>watch监听</div><br/>
  <div @click="count++" style="cursor: pointer;">点击增加: {{count}}</div>
</template>

<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name: "WatchPra"
})
</script>
<script setup lang="ts">
import {ref,watch} from 'vue';
let count  = ref<number>(0);
watch(count,(newVal,oldVal)=>{
  console.log('监听到数据:',newVal,oldVal)
},{immediate:true})
</script>

运行结果

watch监听ref复杂类型数据

  • 直接监听ref对象,只能监听到当前ref对象的值被替换
  • 如果想监听ref代理对象的改变,则需要设置deep进行深度监听
  • 直接监听ref对象的value值,会默认开启深度监听
  • 监听ref代理对象内部的某个属性值,值是复杂数据类型时,默认开启深度监听,如果参数一使用函数式写法,不会默认开启深度监听
  • 监听ref对象内部某个属性值,值是基本数据类型时,写法参数一使用函数式写法,默认开启深度监听
<template>
  <div>watch监听</div><br/>
<!--  <div @click="count++" style="cursor: pointer;">点击增加: {{count}}</div>-->
  <div @click="cat.age++" style="cursor: pointer;">点击增加: {{cat}}</div>
</template>

<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name: "WatchPra"
})
</script>
<script setup lang="ts">
import {ref,watch} from 'vue';
// let count  = ref<number>(0);
// watch(count,(newVal,oldVal)=>{
//   console.log('监听到数据:',newVal,oldVal)
// },{immediate:true})
let cat = ref<object>({name:'小五',age:3,color:'orange'})
watch(cat,(newVal,oldVal)=>{
  console.log('监听到变化: ', {cat})
})
</script>

直接监听ref对象运行结果

<template>
  <div>watch监听</div><br/>
<!--  <div @click="count++" style="cursor: pointer;">点击增加: {{count}}</div>-->
  <div @click="cat.age++" style="cursor: pointer;">点击增加: {{cat}}</div>
  <div @click="cat={name:'小五',age:3,color:'white'}" style="cursor: pointer;">点击替换: {{cat}}</div>
</template>

<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name: "WatchPra"
})
</script>
<script setup lang="ts">
import {ref,watch} from 'vue';
// let count  = ref<number>(0);
// watch(count,(newVal,oldVal)=>{
//   console.log('监听到数据:',newVal,oldVal)
// },{immediate:true})
let cat = ref<object>({name:'小五',age:3,color:'orange'})
watch(cat,(newVal,oldVal)=>{
  console.log('监听到变化: ', {cat})
})
</script>

监听ref对象值被替换运行结果

<template>
  <div>watch监听</div><br/>
<!--  <div @click="count++" style="cursor: pointer;">点击增加: {{count}}</div>-->
<!--  <div @click="cat.age++" style="cursor: pointer;">点击增加: {{cat}}</div>-->
<!--  <div @click="cat={name:'小五',age:3,color:'white'}" style="cursor: pointer;">点击替换: {{cat}}</div>-->
  <div @click="cat.active.push('抓老鼠')" style="cursor: pointer;">点击添加数据: {{cat}}</div>
</template>

<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name: "WatchPra"
})
</script>
<script setup lang="ts">
import {ref,watch} from 'vue';
// let count  = ref<number>(0);
// watch(count,(newVal,oldVal)=>{
//   console.log('监听到数据:',newVal,oldVal)
// },{immediate:true})
// let cat = ref<object>({name:'小五',age:3,color:'orange'})
// watch(cat,(newVal,oldVal)=>{
//   console.log('监听到变化: ', {cat})
// })
let cat = ref<object>({name:'小五',age:3,color:'orange',active:['睡','吃']})
watch(cat.value,(newVal,oldVal)=>{
  console.log('监听到变化: ', {cat})
})
</script>

监听value值验证默认监听效果运行结果

<template>
  <div>watch监听</div><br/>
<!--  <div @click="count++" style="cursor: pointer;">点击增加: {{count}}</div>-->
<!--  <div @click="cat.age++" style="cursor: pointer;">点击增加: {{cat}}</div>-->
<!--  <div @click="cat={name:'小五',age:3,color:'white'}" style="cursor: pointer;">点击替换: {{cat}}</div>-->
<!--  <div @click="cat.active.push('抓老鼠')" style="cursor: pointer;">点击添加数据: {{cat}}</div>-->
  <div @click="person.reactive.son = '小小姜'" style="cursor: pointer;">点击改变数据: {{person}}</div>
</template>

<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name: "WatchPra"
})
</script>
<script setup lang="ts">
import {ref,watch} from 'vue';
// let count  = ref<number>(0);
// watch(count,(newVal,oldVal)=>{
//   console.log('监听到数据:',newVal,oldVal)
// },{immediate:true})
// let cat = ref<object>({name:'小五',age:3,color:'orange'})
// watch(cat,(newVal,oldVal)=>{
//   console.log('监听到变化: ', {cat})
// })
// let cat = ref<object>({name:'小五',age:3,color:'orange',active:['睡','吃']})
// watch(cat.value,(newVal,oldVal)=>{
//   console.log('监听到变化: ', {cat})
// })
let person = ref({name:'老姜',age:18,sex:'男',reactive:{ son:'小姜', sister:'小姜妹' }});
watch(()=>person.value.reactive.son,(newVal,oldVal)=>{
  console.log('监听到: ',person.value.reactive)
})
</script>

运行结果

监听reactive定义数据

  • 监听reactive定义的代理数据,写法和逻辑和监听ref定义复杂数据的代理数据一样

watch监听高级用法

  • 同时监听多个数据时,参数一写为数组形式
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值