Vue3中watch的使用詳解
Vue3中watch的詳解
Vue2使用watch
<template> <div>總合:{{ sum }}<button @click="sum++">點(diǎn)擊累加</button></div> </template> <script> import { ref } from "vue"; export default { // vue2中使用watch watch: { sum: { deep: true, handler(newValue, oldValue) { console.log("總合 sum 變化:", newValue, oldValue); }, }, }, setup() { let sum = ref(0); return { sum, }; }, }; </script> <style> </style>
Vue3使用watch
watch有三個(gè)參數(shù):
參數(shù)1:監(jiān)聽(tīng)的參數(shù)
參數(shù)2:監(jiān)聽(tīng)的回調(diào)函數(shù)
參數(shù)3:監(jiān)聽(tīng)的配置(immediate)
情況1
監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù)
<template> <div>總合:{{ sum }}<button @click="sum++">點(diǎn)擊累加</button></div> </template>
// 監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù) <script> import { ref, watch } from "vue"; export default { setup() { let sum = ref(0); // 監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù) watch(sum, (newValue, oldValue) => { console.log("sum ==> ", newValue, oldValue); }); return { sum, }; }, }; </script>
情況2
監(jiān)視r(shí)ef所定義的多個(gè)響應(yīng)式數(shù)據(jù)
<template> <div>總合:{{ sum }}<button @click="sum++">點(diǎn)擊累加</button></div> <hr /> <div> msg:{{ msg }} <button @click="msg += '~'">改變msg</button> </div> </template>
<script> import { ref, watch } from "vue"; export default { setup() { let sum = ref(0); let msg = ref("watch使用"): // 情況2:監(jiān)視r(shí)ef所定義的多個(gè)響應(yīng)式數(shù)據(jù) watch([sum, msg], (newValue, oldValue) => { console.log("sum/msg ==> ", newValue, oldValue); },{immediate:true}); return { sum, msg, }; }, }; </script>
情況3
監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)
注意:
- 這里無(wú)法正確獲取oldValue
- 強(qiáng)制開(kāi)啟了深度監(jiān)聽(tīng)(deep配置不生效)
<template> <div> <h3>情況3::監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)</h3> <div>姓名:{{person.name}}</div> <div>年齡:{{person.age}}</div> <button @click="person.name += '~'">修改姓名</button> <button @click="person.age ++">修改年齡</button> </div> </template>
<script> import { ref, watch,reactive } from "vue"; export default { setup() { let person = reactive({ name: "lisa", age: 18, job: { joblist: { money: 10, }, }, }); // 情況3、監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù) /* 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù),則無(wú)法正確獲得oldvalue! 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù),則強(qiáng)制開(kāi)啟了深度監(jiān)視 */ watch(person,(newValue, oldValue) => { console.log("person ==> ", newValue, oldValue); },{immediate:true,deep:false}//這里的deep配置不再奏效 ); return { person, }; }, }; </script>
情況4
監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性
<template> <div> <h3>情況4::監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性</h3> <div>姓名:{{person.name}}</div> <div>年齡:{{person.age}}</div> <button @click="person.name += '~'">修改姓名</button> <button @click="person.age ++">修改年齡</button> </div> </template>
<script> import { ref, watch,reactive } from "vue"; export default { setup() { let person = reactive({ name: "lisa", age: 18, job: { joblist: { money: 10, }, }, }); // 情況4、監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性 watch(()=>person.name,(newValue, oldValue) => { console.log("person.name ==> ", newValue, oldValue); }); return { person, }; }, }; </script>
情況5
監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某些屬性
<template> <div> <h3>情況4::監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性</h3> <div>姓名:{{person.name}}</div> <div>年齡:{{person.age}}</div> <button @click="person.name += '~'">修改姓名</button> <button @click="person.age ++">修改年齡</button> </div> </template>
<script> import { ref, watch,reactive } from "vue"; export default { setup() { let person = reactive({ name: "lisa", age: 18, job: { joblist: { money: 10, }, }, }); // 情況5、監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某些屬性 watch([()=>person.name,()=>person.age],(newValue, oldValue) => { console.log("person.name/person.age ==> ", newValue, oldValue); }); return { person, }; }, }; </script>
特殊情況
watch監(jiān)聽(tīng)reactive中對(duì)象的嵌套對(duì)象
<template> <div> <div>姓名:{{person.name}}</div> <div>年齡:{{person.age}}</div> <div>薪資:{{person.job.joblist.money}} K</div> <button @click="person.name += '~'">修改姓名</button> <button @click="person.age ++">修改年齡</button> <button @click="person.job.joblist.money ++">提薪</button> </div> </template>
<script> import { ref, watch,reactive } from "vue"; export default { setup() { let person = reactive({ name: "lisa", age: 18, job: { joblist: { money: 10, }, }, }); // 特殊情況、監(jiān)視r(shí)eactive所定義嵌套對(duì)象 watch(()=>person.job,(newValue, oldValue) => { console.log("person.job對(duì)象發(fā)生變化 ==> ", newValue, oldValue); },{deep:true});//此處由于監(jiān)視的是reactive素定義的對(duì)象中的某個(gè)屬性,所以deep配置有效 return { person, }; }, }; </script>
到此這篇關(guān)于Vue3中watch的詳解的文章就介紹到這了,更多相關(guān)Vue3 watch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)彈出框點(diǎn)擊空白頁(yè)彈框消失效果
這篇文章主要介紹了VUE實(shí)現(xiàn)彈出框點(diǎn)擊空白頁(yè)彈框消失,實(shí)現(xiàn)方法可以在Vue中實(shí)現(xiàn)彈出框然后通過(guò)點(diǎn)擊空白頁(yè)面來(lái)讓彈窗隱藏,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12vue動(dòng)畫之點(diǎn)擊按鈕往上漸漸顯示出來(lái)的實(shí)例
今天小編就為大家分享一篇vue動(dòng)畫之點(diǎn)擊按鈕往上漸漸顯示出來(lái)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09關(guān)于vue3默認(rèn)把所有onSomething當(dāng)作v-on事件綁定的思考
這篇文章主要介紹了關(guān)于vue3默認(rèn)把所有`onSomething`當(dāng)作`v-on`事件綁定的思考,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果
今天小編就為大家分享一篇vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue中this.$router.push()路由傳值和獲取的兩種常見(jiàn)方法匯總
這篇文章主要介紹了vue中this.$router.push()路由傳值和獲取的兩種常見(jiàn)方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12vue控制臺(tái)警告Runtime directive used on compon
這篇文章主要為大家介紹了vue控制臺(tái)警告Runtime directive used on component with non-element root node解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Vue Element-UI中el-table實(shí)現(xiàn)單選的示例代碼
在element-ui中是為我們準(zhǔn)備好了可直接使用的單選與多選屬性的,本文主要介紹了Vue Element-UI中el-table實(shí)現(xiàn)單選的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12vue element-ui 綁定@keyup事件無(wú)效的解決方法
遇到vue element-ui 綁定@keyup事件無(wú)效的問(wèn)題怎么解決?下面小編就為大家分享一篇vue element-ui 綁定@keyup事件無(wú)效的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue中watch監(jiān)聽(tīng)器用法之deep、immediate、flush
Vue是可以監(jiān)聽(tīng)到多層級(jí)數(shù)據(jù)改變的,且可以在頁(yè)面上做出對(duì)應(yīng)展示,下面這篇文章主要給大家介紹了關(guān)于vue中watch監(jiān)聽(tīng)器用法之deep、immediate、flush的相關(guān)資料,需要的朋友可以參考下2022-09-09