watch侦听器
Vue3 监听属性 watch,我们可以通过 watch 来响应数据的变化。
定义方式和计算属性类似
通过watch监听
<script setup>
import { watch,ref } from 'vue';
const userName = ref('') // 这里不能使用语法糖
watch(userName,(newValue,oldValue)=>{
console.log('新的值:'+newValue+' 旧的值:'+oldValue)
})
</script>
<template>
<input v-model="userName" />
</template>
使用场景
侦听数据异常:
1、输入数据判断 ——验证用户名是否可用
<script setup>
import { watch,ref } from 'vue';
const userName = ref('