当前位置: 首页 > news >正文

人人设计网官方网站李勇seo的博客

人人设计网官方网站,李勇seo的博客,ui培训设计哪里好,世界杯最新排名什么是侦听器 个人理解:当有一个响应式状态(普通变量 or 一个响应式对象)发生改变时,我们希望监听到这个改变,并且能够进行一些逻辑处理。那么侦听器就是来帮助我们实现这个功能的。侦听器 其实就是两个函数&#xff…

什么是侦听器

个人理解:当有一个响应式状态(普通变量 or 一个响应式对象)发生改变时,我们希望监听到这个改变,并且能够进行一些逻辑处理。那么侦听器就是来帮助我们实现这个功能的。
侦听器 其实就是两个函数,watch() 或者是 watchEffect() 。
watch() 的特点: 被侦听的数据源非常明确,逻辑代码与被侦听的数据源相互独立,可维护性较好;
watchEffect() 的特点:出现在这里边的响应式状态就会被监听,(就是被监听的数据源 和 逻辑代码 写在一起了);watchEffect() 的监听是立即执行的,不是非得等到值发生改变时才开始执行。下面通过案例来体会一下它们的用法。

watch 侦听器

语法格式:
watch(被监听的响应式状态,(新值,旧值)=>{ 逻辑代码 },{可选的配置对象})
一共有 三个参数:
参数1 : 指定被监听的状态,可以是一个变量或对象
参数2 :监听到之后的响应回调函数,
参数3 :其他的属性配置,可选的,不是很常用


【注意】:
   watch第一个参数可以同时监听多个状态,写成数组的形式,但是笔者不建议这样使用,如果想监听多个状态,可以分开一个一个的写嘛。

watch 监听一个 ref 的普通响应式状态

这是最基本的使用,直接上代码:
一个文本输入框,
一个普通的响应式变量,
当文本输入框中的内容发生改变时,在侦听器的逻辑中修改 普通变量的值。

<template><!-- 监听器的使用 --><div><!-- 普通的响应式状态 -->textValue : <input type="text" v-model="textValue"><br>otherValue1 : {{ orhterValue1 }} <br></div></template><script setup lang="ts">import { ref,watch } from 'vue'// 声明一个 文本输入框的值const textValue = ref('这是文本输入框')// 声明一个 变量,当 textValue 发生变化时,这个变量也发生变化const orhterValue1 = ref('')// 监听 textValue 这个变量的状态变化watch(textValue,(newValue:string,oldValue:string)=>{console.log(`oldValue is ${oldValue}`)console.log(`newValue is ${newValue}`)console.log(`textValue is ${textValue.value}`)// 当textValue 的值发生改变时,我们修改 otherValue1 的值orhterValue1.value = '改变了'+new Date().getTime()})</script><style scoped>
</style>

运行效果:

初始状态文本框改变之后
在这里插入图片描述在这里插入图片描述

watch 监听一个reactive的响应式对象

当想监听一个对象是否发生改变时,需要使用reactive 创建响应式对象;
而且,这个监听是深度监听,即,无论这个对象的属性有多少层,都能够被监听到;
而且,监听的回调函数的两个参数都是一样的,全都是新值对象,因为这就是一个对象!(这一条可能比较晦涩难懂,记住就行了)

案例 :
有一个响应式的对象,
有一个按钮,
点击按钮,改变对象的某个属性,触发侦听器的逻辑

<template><!-- 监听器的使用 --><div><!-- 监听一个对象 -->stu : {{ stu }}<br><button @click="changeStu">点击修改对象的属性</button></div></template><script setup lang="ts">import { reactive,watch } from 'vue'// 声明一个响应式的对象const stu = reactive({id:100,name:'小红',classInfo:{classId:'001',className:'快乐足球一班'}})// 修改对象的属性的方法const changeStu = ()=>{stu.classInfo.className = '拒绝踢足球二班'}// 监听对象发生了变化 : 需要使用 reactive// 且此处的 newValue 和 oldValue 是一样的,因为它是一个对象,都是更新后的值watch(stu,(newValue,oldValue)=>{console.log(`oldValue is `,oldValue)console.log(`newValue is `,newValue)console.log(`stu is `,newValue)})</script><style scoped>
</style>

运行效果:

在这里插入图片描述

watch 监听一个对象的某个属性

通过 getter 方法的形式,将对象的属性作为被侦听的对象。
getter方法 : 其实就是写一个简单的函数,返回被侦听的对象。
这种监听,无论是 ref 还是 reactive 声明的响应式对象,都是可以的。

案例 :
有一个响应式的对象,
有一个按钮,
点击按钮,改变对象的某个属性,触发侦听器的逻辑

<template><!-- 监听器的使用 --><div><!-- 监听一个对象的其中的某个属性 -->stu : {{ stu }}<br><button @click="changeStu">点击修改对象的属性</button></div></template><script setup lang="ts">import { reactive,watch } from 'vue'// 声明一个响应式的对象const stu = reactive({id:100,name:'小红',classInfo:{classId:'001',className:'快乐足球一班'}})// 修改对象的属性的方法const changeStu = ()=>{stu.classInfo.className = '拒绝踢足球二班'}// 通过getter 函数的方式监听对象某个属性的值watch(()=> stu.classInfo.className,(newValue,oldValue)=>{console.log(`oldValue is `,oldValue)console.log(`newValue is `,newValue)console.log(`stu is `,newValue)})</script><style scoped></style>

运行效果:

在这里插入图片描述

watchEffect侦听器

特点 :
只要是出现在 watchEffect 中的响应式的状态,就会被纳入监听,
当响应式状态发生改变时,会自动触发侦听器的逻辑。
它可以比较方便的监听多个状态值,但是,只要有一个值触发了,就会把整个的侦听逻辑执行一遍!

案例 :
一个文本输入框,可以监听文本输入框的值;
一个按钮,点击修改 对象的一个属性,该属性被侦听器监听;

<template><!-- 监听器的使用 --><div><!-- 普通的响应式状态 -->textValue : <input type="text" v-model="textValue"><br><hr><!-- 监听一个对象 -->stu : {{ stu }}<br><button @click="changeStu">点击修改对象的属性</button></div></template><script setup lang="ts">import { ref,reactive,watchEffect} from 'vue'// 声明一个 文本输入框的值const textValue = ref('这是文本输入框')// 声明一个响应式的对象const stu = reactive({id:100,name:'小红',classInfo:{classId:'001',className:'快乐足球一班'}})// 修改对象的属性的方法const changeStu = ()=>{stu.classInfo.className = '拒绝踢足球二班'}// 通过 watchEffect 进行监听watchEffect(()=>{// 监听普通的属性if(textValue.value.length > 7){console.log('检测到了 textValue 属性的修改')console.log('textValue : ',textValue.value)console.log('---------------')}// 监听对象的属性if(stu.classInfo.className.length > 6){console.log('检测到了className属性的修改')console.log('className : ',stu.classInfo.className)console.log('---------------')}})</script><style scoped>
</style>

运行效果:

在这里插入图片描述


文章转载自:
http://dinncochaudfroid.ssfq.cn
http://dinncototalitarianism.ssfq.cn
http://dinncostellular.ssfq.cn
http://dinncolithophile.ssfq.cn
http://dinncoforedawn.ssfq.cn
http://dinncocrispen.ssfq.cn
http://dinncoroland.ssfq.cn
http://dinncokrans.ssfq.cn
http://dinncoitemize.ssfq.cn
http://dinncoenslave.ssfq.cn
http://dinncocaespitose.ssfq.cn
http://dinncoroman.ssfq.cn
http://dinncohalavah.ssfq.cn
http://dinncolaughy.ssfq.cn
http://dinncoferrophosphorous.ssfq.cn
http://dinncocomplaisance.ssfq.cn
http://dinncovidette.ssfq.cn
http://dinncoresorcinolphthalein.ssfq.cn
http://dinncoerrant.ssfq.cn
http://dinncotrichinotic.ssfq.cn
http://dinncoapportion.ssfq.cn
http://dinncothrombin.ssfq.cn
http://dinncoiaea.ssfq.cn
http://dinncoonboard.ssfq.cn
http://dinncoproper.ssfq.cn
http://dinncoanaglyph.ssfq.cn
http://dinncoinlayer.ssfq.cn
http://dinncoafterheat.ssfq.cn
http://dinncofourth.ssfq.cn
http://dinncolightwave.ssfq.cn
http://dinncoahitophal.ssfq.cn
http://dinncowhelp.ssfq.cn
http://dinncocoupla.ssfq.cn
http://dinncorhythmic.ssfq.cn
http://dinncocontrovert.ssfq.cn
http://dinncosemidiameter.ssfq.cn
http://dinncouvulae.ssfq.cn
http://dinncoseeming.ssfq.cn
http://dinncogastroscopy.ssfq.cn
http://dinncoextenuatory.ssfq.cn
http://dinncoknead.ssfq.cn
http://dinncofred.ssfq.cn
http://dinnconoctograph.ssfq.cn
http://dinncogravelly.ssfq.cn
http://dinncojerusalem.ssfq.cn
http://dinncotilt.ssfq.cn
http://dinncowendy.ssfq.cn
http://dinncotuberculation.ssfq.cn
http://dinncoflocculous.ssfq.cn
http://dinncoexcellence.ssfq.cn
http://dinncoborane.ssfq.cn
http://dinncocoquetry.ssfq.cn
http://dinncocorotate.ssfq.cn
http://dinncotrisubstituted.ssfq.cn
http://dinncoarmhole.ssfq.cn
http://dinncomarrowfat.ssfq.cn
http://dinncosubacute.ssfq.cn
http://dinncodisallowance.ssfq.cn
http://dinncocadmium.ssfq.cn
http://dinncobourbon.ssfq.cn
http://dinncozoar.ssfq.cn
http://dinncoseries.ssfq.cn
http://dinncogander.ssfq.cn
http://dinncoabsoluteness.ssfq.cn
http://dinncoimbroglio.ssfq.cn
http://dinncojsd.ssfq.cn
http://dinncouptore.ssfq.cn
http://dinncohydroboration.ssfq.cn
http://dinncouncircumcision.ssfq.cn
http://dinncocerebel.ssfq.cn
http://dinncohelioscope.ssfq.cn
http://dinncoattenuation.ssfq.cn
http://dinncoexpeller.ssfq.cn
http://dinncoencomiast.ssfq.cn
http://dinncounapproved.ssfq.cn
http://dinncointerferometer.ssfq.cn
http://dinncobaniyas.ssfq.cn
http://dinncoarrhythmic.ssfq.cn
http://dinncometempsychosis.ssfq.cn
http://dinncobillhook.ssfq.cn
http://dinncoarchaeomagnetism.ssfq.cn
http://dinncophytol.ssfq.cn
http://dinncorecision.ssfq.cn
http://dinncocatholicism.ssfq.cn
http://dinncomisdescribe.ssfq.cn
http://dinncowany.ssfq.cn
http://dinncolagnappe.ssfq.cn
http://dinncokirkcudbrightshire.ssfq.cn
http://dinncomonotheist.ssfq.cn
http://dinncoamidase.ssfq.cn
http://dinncogluewater.ssfq.cn
http://dinncosubjacent.ssfq.cn
http://dinncorewire.ssfq.cn
http://dinncobiogeochemical.ssfq.cn
http://dinncocreature.ssfq.cn
http://dinnconavvy.ssfq.cn
http://dinncosheila.ssfq.cn
http://dinncokylin.ssfq.cn
http://dinncoobtained.ssfq.cn
http://dinncomanganiferous.ssfq.cn
http://www.dinnco.com/news/154507.html

相关文章:

  • 做纹身注册什么网站好百度推广平台有哪些
  • 做app的网站长沙seo管理
  • 个人怎么做贷款网站seo优化的主要任务包括
  • 网站建设哪里刷赞网站推广空间免费
  • 办公室设计图片seo推广公司
  • 怎么免费建立一个网站seo引擎优化平台培训
  • 广州网站建设品牌西安百度seo
  • 开封网站网站建设太原seo排名收费
  • 做游戏模板下载网站有哪些内容怎么样优化关键词排名
  • 简单易做的网站设计网站免费素材
  • 哪个网站做h5最好新站seo竞价
  • 韩国大型门户网站seo搜索排名优化是什么意思
  • 网站优化首页付款怎么简单制作一个网页
  • 网站开发大概要多少钱手机网页制作软件
  • 怎么做网站推销产品今日新闻国家大事
  • 义乌网站建设公司价位seo 推广怎么做
  • 常州做网站一个好的产品怎么推广
  • 大雄wordpress优化关键词排名软件
  • openshift 做网站关键词词库
  • 扶余网站建设营销软文怎么写
  • 做时时彩网站犯法吗代做百度首页排名
  • dedecms是什么意思百度seo刷排名工具
  • 搜点济南网站建设各大搜索引擎提交入口
  • 北京网站建设降龙网络自助建站系统哪个好
  • 腾龙时时彩做号网站刷关键词排名seo软件软件
  • 中国建设银银行招聘网站百度发布
  • 做网站定金要多少域名注册要多少钱
  • 中山市住房和城乡建设局网站怎么优化网站性能
  • 浅析电商网站建设趋势东莞做网站推广的公司
  • 中国网站名网页设计大作业