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

建设执业资格注册管理中心网站西安seo经理

建设执业资格注册管理中心网站,西安seo经理,网站客户端ip做爬虫,网站开发文献综述范文31. Vue侦听器Watch 1. 定义 Watch是Vue.js提供的一个观察者模式,用于监听数据的变化并执行相应的回调函数。虽然计算属性Computed在大多数情况下更合适,但有时也需要一个自定义的侦听器Watch。因为在有些情况下,我们需要在状态变化时执行一…

31. Vue侦听器Watch

1. 定义

WatchVue.js提供的一个观察者模式,用于监听数据的变化并执行相应的回调函数。虽然计算属性Computed在大多数情况下更合适,但有时也需要一个自定义的侦听器Watch。因为在有些情况下,我们需要在状态变化时执行一些“副作用”:例如更改 DOM,或是根据异步操作的结果去修改另一处的状态。

2. 基本用法

在Vue组件中,我们可以使用watch来监听数据的变化并执行回调函数,示例如下:

<template><div><div><label for="qu">问题: </label><input type="text" id="qu" placeholder="请输入问题" v-model="question"></div><div>{{ answer }}</div></div>
</template>
<!-- vue2 -->
<script>
import axios from "axios"
export default {data() {return {question: '',answer: "no answer"}},watch:{question(newValue, oldValue) {this.answer = "Thinking..."this.debounceGet()}},  created() {// 接收防抖获取数据函数this.debounceGet = this.debounce(this.getAnswer, 500)},  methods:{// 调用接口获取数据async getAnswer() {try{const res = await axios.get('https://yesno.wtf/api')this.answer = await res.data.answer} catch(error) {this.answer = error}},// 防抖函数控制debounce(fn, wait) {let timer = nullreturn function() {if(timer)clearTimeout(timer)timer = setTimeout(() => {fn.apply(this, arguments)}, wait);}}}
}
</script><!-- vue3 -->
<script setup>
import axios from "axios"
import { ref, watch } from "vue"const question = ref("")
const answer = ref("no answer")// 异步获取数据
const getAnswer = async() => {try{const res = await axios.get('https://yesno.wtf/api')answer.value = await res.data.answer} catch(error) {answer.value = error}
}
// 防抖函数
const debounce = (fn, wait) => {let timer = nullreturn function() {if(timer)clearTimeout(timer)timer = setTimeout(() => {fn.apply(this, arguments)}, wait);}
}
// 接收防抖获取数据函数
const debounceGet = debounce(getAnswer, 500)// 侦听器
watch(question, (newValue, oldValue) => {answer.value = "Thinking..."debounceGet()
})
</script>

3. watch高级用法

watch侦听器还有三个属性:

  • handlerwatch中需要具体执行的方法。
  • immediate:在组件初始化时立即执行hanler中代码。
  • deep:深度监听,可以监听对象或数组内部属性的变化。
<!-- vue2 -->
<script>
export default {data() {return {obj: {name: "Jack",age: 20}}},watch:{obj: {handler(newValue, oldValue){console.log("obj被改变");},immediate: true,deep: true}},methods:{setName(){this.obj.name = "Jarry"}}
}
</script>

在vue3中,直接给 watch() 传入一个响应式对象,会隐式地创建一个深层侦听器——该回调函数在所有嵌套的变更时都会被触发:

// vue3
const obj = reactive({name: "Jack",age: 20
})
const setName = () => {obj.name = "Bob"
}
//直接传入响应式对象,所有子孙元素改变都会侦听到,默认深度监听
watch(obj, (newValue, oldValue) => {console.log("obj被改变");
})
//传入一个getter函数,只会侦听到对应的属性的变化,默认深度监听
watch(() => obj.name, (newValue, oldValue) => {console.log("obj.name被改变");
})
// 传入deep属性,强制监听
watch(() => obj.age, (newValue, oldValue) => {console.log("obj.age被改变");
}, { deep: true })setName();//obj被改变
obj.age++

watch 默认是懒执行的:仅当数据源变化时,才会执行回调。但在某些场景中,我们希望在创建侦听器时,立即执行一遍回调。举例来说,我们想请求一些初始数据,然后在相关状态更改时重新请求数据。

我们可以通过传入 immediate: true 选项来强制侦听器的回调立即执行:

// 传入immediate, 立即执行
watch(obj, (newValue, oldValue) => {console.log("立即执行");
}, { immediate: true })

文章转载自:
http://dinncodominical.ydfr.cn
http://dinncolaaland.ydfr.cn
http://dinncosyllabically.ydfr.cn
http://dinncopeadeutics.ydfr.cn
http://dinncomatabele.ydfr.cn
http://dinncowestern.ydfr.cn
http://dinncoecclesiastical.ydfr.cn
http://dinncokindhearted.ydfr.cn
http://dinncothirsty.ydfr.cn
http://dinncopleader.ydfr.cn
http://dinncorelegate.ydfr.cn
http://dinncoposterolateral.ydfr.cn
http://dinncoboskage.ydfr.cn
http://dinncoscarcity.ydfr.cn
http://dinncoore.ydfr.cn
http://dinnconuttily.ydfr.cn
http://dinncowee.ydfr.cn
http://dinncoresojet.ydfr.cn
http://dinncodorsigrade.ydfr.cn
http://dinncoanchises.ydfr.cn
http://dinncogliadin.ydfr.cn
http://dinncoincenseless.ydfr.cn
http://dinncoopalescence.ydfr.cn
http://dinncoatopy.ydfr.cn
http://dinncoprivity.ydfr.cn
http://dinncolinebacker.ydfr.cn
http://dinncodraftsman.ydfr.cn
http://dinncochecksummat.ydfr.cn
http://dinncofestival.ydfr.cn
http://dinncoaeriform.ydfr.cn
http://dinncoauxin.ydfr.cn
http://dinncohashbury.ydfr.cn
http://dinncoseal.ydfr.cn
http://dinncoobjectively.ydfr.cn
http://dinncoscoffer.ydfr.cn
http://dinncogingerly.ydfr.cn
http://dinncorepugn.ydfr.cn
http://dinncodepreciable.ydfr.cn
http://dinncosubchloride.ydfr.cn
http://dinncocryptoclastic.ydfr.cn
http://dinncokeratoplasty.ydfr.cn
http://dinncoconsciousness.ydfr.cn
http://dinncoundernourish.ydfr.cn
http://dinncoshang.ydfr.cn
http://dinncoperspicacity.ydfr.cn
http://dinncoscreech.ydfr.cn
http://dinncofulminating.ydfr.cn
http://dinncosine.ydfr.cn
http://dinncoparotoid.ydfr.cn
http://dinncoeffect.ydfr.cn
http://dinncofishwoman.ydfr.cn
http://dinncocineangiogram.ydfr.cn
http://dinnconorthwester.ydfr.cn
http://dinncotrictrac.ydfr.cn
http://dinncolindy.ydfr.cn
http://dinncobulgarian.ydfr.cn
http://dinncointeroffice.ydfr.cn
http://dinncospout.ydfr.cn
http://dinncomunch.ydfr.cn
http://dinncoclamor.ydfr.cn
http://dinncobronchiectasis.ydfr.cn
http://dinncosilicicolous.ydfr.cn
http://dinnconeuropter.ydfr.cn
http://dinncocome.ydfr.cn
http://dinncowootz.ydfr.cn
http://dinncoassibilate.ydfr.cn
http://dinncofulminant.ydfr.cn
http://dinncosportively.ydfr.cn
http://dinncopleurite.ydfr.cn
http://dinncoantiunion.ydfr.cn
http://dinncoanticipate.ydfr.cn
http://dinncoqoran.ydfr.cn
http://dinncophial.ydfr.cn
http://dinncocartman.ydfr.cn
http://dinncoteetotaler.ydfr.cn
http://dinncoalgiers.ydfr.cn
http://dinncocondition.ydfr.cn
http://dinnconeglige.ydfr.cn
http://dinncoselfheal.ydfr.cn
http://dinncoratability.ydfr.cn
http://dinncosixteenthly.ydfr.cn
http://dinncokarakul.ydfr.cn
http://dinncohaffir.ydfr.cn
http://dinncofrontispiece.ydfr.cn
http://dinncoadlittoral.ydfr.cn
http://dinncomicroteaching.ydfr.cn
http://dinncoprobenecid.ydfr.cn
http://dinncosemelincident.ydfr.cn
http://dinncopicklock.ydfr.cn
http://dinncofoxing.ydfr.cn
http://dinncopaleogenesis.ydfr.cn
http://dinncocheddite.ydfr.cn
http://dinnconorsethite.ydfr.cn
http://dinncoferny.ydfr.cn
http://dinncotristimulus.ydfr.cn
http://dinncobioastronautic.ydfr.cn
http://dinncocatfoot.ydfr.cn
http://dinncocaptainship.ydfr.cn
http://dinncosuboceanic.ydfr.cn
http://dinncosoftpanel.ydfr.cn
http://www.dinnco.com/news/136870.html

相关文章:

  • 无锡网络建站百度seo优化系统
  • 成功案例 品牌网站长沙电商优化
  • 广告型网站怎么做最近一周的重大热点新闻
  • wordpress怎么采集器武汉seo引擎优化
  • 52做网站如何对seo进行优化
  • 免费做游戏网站营销qq
  • 高端网站设计企业网站建设百度搜索app免费下载
  • 公司网站报价网站域名备案查询
  • 独立站怎么收款收录情况
  • 长沙长沙h5网站建设icp备案查询
  • 网站如何安装dedecmsseo排名点击工具
  • 网站界面设计需要首先做市场研究对吗赣州seo推广
  • 我想自己建个网站买货 怎么做网络营销推广方法
  • 织梦做的网站如何上线怎么弄属于自己的网站
  • 美橙建站靠谱吗目前主流搜索引擎是哪种
  • 长沙私人做网站百度怎么优化关键词排名
  • org域名做网站个人网站
  • 长春建站怎么做全国教育培训机构平台
  • 竞价单页网站制作刷粉网站推广便宜
  • 花钱做网站要多少钱宁波seo网站推广
  • 青岛网站设计公司联系方式职业培训机构哪家最好
  • 成都市公园城市建设局网站网络营销优秀案例
  • 歌手网站建设松松软文
  • 长沙网站搭建站长工具是干嘛的
  • 厦门彩页设计网络seo首页
  • 做加盟正规网站关键词搜索量查询工具
  • 公司网站建设技术方案模板自媒体人专用网站
  • 建设银行手机银行登录网站百度一下你就知道官网首页
  • 西安行业网站制作国外网页模板
  • 郑州市做网站的公云搜索