Vue3中的?computed,watch,watchEffect的使用方法
一、computed
<template> 姓:<input v-model="person.firstName"><br/><br/> 名:<input v-model="person.lastName"><br/><br/> <span>全名:{{person.fullname}}</span><br/><br/> <span>全名:<input v-model="person.fullname"></span> </template> <script> import {reactive,computed} from 'vue' export default { name: 'HelloWorld', setup(){ let person = reactive({ firstName:"張", lastName:"三" }) //computed簡寫形式,沒考慮修改 /*person.fullname = computed(()=>{ return person.firstName+"-"+person.lastName; })*/ person.fullname = computed({ get(){ return person.firstName+"-"+person.lastName; }, set(value){ const nameArr = value.split('-'); person.firstName = nameArr[0]; person.lastName = nameArr[1]; } }) return{ person, } } } </script>
二、watch
- 1、與 Vue2.x 中 watch 配置功能一致
- 2、兩個(gè)小"坑":
- 監(jiān)視 reactive 定義的響應(yīng)式數(shù)據(jù)時(shí): oldValue 無法正確獲取、強(qiáng)制開啟了深度監(jiān)視(deep配置失效)
- 監(jiān)視 reactive 定義的響應(yīng)式數(shù)據(jù)中某個(gè)屬性時(shí):deep 配置有效
我們?nèi)匀蛔鲋扒蠛偷陌咐?/strong>
vu2 的寫法
<template> <h2>當(dāng)前求和為:{{ sum }}</h2> <button @click="sum++">點(diǎn)我sum++</button> </template> <script> import {ref} from 'vue' export default { name: 'Demo', watch: { /*sum(oldValue,newValue){ console.log("sum發(fā)生了變化",oldValue,newValue); }*/ sum: { immediate: true, deep:true, handler(newValue,oldValue) { console.log("sum發(fā)生了變化", newValue, oldValue); } } }, setup() { let sum = ref(0); return { sum, } } } </script>
Vue3 中這樣寫
1、情況一:監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù)
<template> <h2>當(dāng)前求和為:{{ sum }}</h2> <button @click="sum++">點(diǎn)我sum++</button>> </template> <script> import {ref, watch} from 'vue' export default { name: 'Demo', setup() { let sum = ref(0); let msg = ref("你好啊"); //情況一:監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù) watch(sum, (newValue, oldValue) => { console.log("sum發(fā)生了變化", newValue, oldValue); }) return { sum } } } </script>
watch 還可以傳一個(gè)配置項(xiàng),把 immediate 等配置傳進(jìn)去:
watch(sum, (newValue, oldValue) => { console.log("sum發(fā)生了變化", newValue, oldValue); },{immediate:true})
2、情況二:當(dāng)有多個(gè)信息需要同時(shí)監(jiān)視時(shí)
<template> <h2>當(dāng)前求和為:{{ sum }}</h2> <button @click="sum++">點(diǎn)我sum++</button> <hr/> <h2>信息為:{{ msg }}</h2> <button @click="msg+='!'">點(diǎn)我sum++</button> </template> <script> import {ref, watch} from 'vue' export default { name: 'Demo', setup() { let sum = ref(0); let msg = ref("你好啊"); //情況二:監(jiān)視r(shí)ef所定義的多個(gè)響應(yīng)式數(shù)據(jù) watch([sum,msg],(newValue, oldValue) => { console.log("sum發(fā)生了變化", newValue, oldValue); }) return { sum, msg } } } </script>
3、情況三:監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)
<template> <h2>姓名:{{ person.name }}</h2> <h2>年齡:{{ person.age }}</h2> <h2>薪資:{{ person.job.j1.salary }}K</h2> <button @click="person.name+='~'">修改姓名</button> <button @click="person.age++">修改年齡</button> <button @click="person.job.j1.salary++">漲薪</button> </template> <script> import {reactive, watch} from 'vue' export default { name: 'Demo', setup() { let person = reactive({ name: "張三", age: 18, job:{ j1:{ salary:20 } } }) //情況三:監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)全部屬性 // 1\注意:無法正確獲取oldvalue // 2\注意:強(qiáng)制開啟了深度監(jiān)視(deep配置無效) watch(person, (newValue, oldValue) => { console.log("person發(fā)生了變化", newValue, oldValue); }) return { person } } } </script>
4、情況四:監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)某個(gè)屬性
//情況四:監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)某個(gè)屬性 watch(()=>person.name, (newValue, oldValue) => { console.log("person的name發(fā)生了變化", newValue, oldValue); })
5、情況五:監(jiān)視 reactive 所定義的一個(gè)響應(yīng)式數(shù)據(jù)某些屬性
//情況五:監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)某個(gè)屬性 watch([()=>person.name,()=>person.age], (newValue, oldValue) => { console.log("person的name或age發(fā)生了變化", newValue, oldValue); })
6、特殊情況,監(jiān)視對(duì)象中的某個(gè)對(duì)象屬性,要開始deep:true
watch(()=>person.job, (newValue, oldValue) => { console.log("person的job發(fā)生了變化", newValue, oldValue); },{deep:true})//由于監(jiān)視的是reactive對(duì)象中的某個(gè)屬性,deep奏效
7、監(jiān)視 ref 定義的對(duì)象響應(yīng)數(shù)據(jù),需要.value或deep:true
let person = ref({ name: "張三", age: 18, job:{ j1:{ salary:20 } } }) watch(person.value, (newValue, oldValue) => { console.log("person的value發(fā)生了變化", newValue, oldValue); }) 或 watch(person, (newValue, oldValue) => { console.log("person的value發(fā)生了變化", newValue, oldValue); },{deep:true})
三、watchEffect
watch
的套路是:既要指明監(jiān)視的屬性,也要指明監(jiān)視的回調(diào)
watchEffect
的套路是:不用指明監(jiān)視哪個(gè)屬性,監(jiān)視的回調(diào)中用到哪個(gè)屬性,那就監(jiān)視哪個(gè)屬性
watchEffect
有點(diǎn)像computed
:
。但computed
注重的計(jì)算出來的值(回調(diào)函數(shù)的返回值),所以必須要寫返回值
。而watchEffect
更注重的是過程(回調(diào)函數(shù)的函數(shù)體),所以不用寫返回值
//watchEffect所指定的回調(diào)中用到的數(shù)據(jù)只要發(fā)生變化,則直接重新執(zhí)行回調(diào) watchEffect(()=>{ const xl = sum.value const x2 = person.age console.log( "watchEffect配置的回調(diào)執(zhí)行了") })
例如還用上邊的例子:
import {reactive,watchEffect} from 'vue' export default { name: 'Demo', setup() { let person = reactive({ name: "張三", age: 18, job:{ j1:{ salary:20 } } }) watchEffect(()=>{ const x1 = person.name; console.log("watchEffect所指定的回調(diào)執(zhí)行了"+x1); }) return { person } } } </script>
可以看到用到了哪個(gè)就監(jiān)視哪個(gè)
最后,我們使用 watch 和 watchEffect 實(shí)現(xiàn)姓名的例子
<template> 姓:<input v-model="person.firstName"> 名:<input v-model="person.lastName"> <span>全名:{{fullName}}</span> <span>全名:<input v-model="fullName"></span> </template> <script lang="ts"> import {defineComponent, reactive, ref,watch,watchEffect} from 'vue'; export default defineComponent({ setup(){ let person = reactive({ firstName:"張", lastName:"三" }); const fullName = ref(''); watch(person,({firstName,lastName})=>{ fullName.value = firstName+"-"+lastName },{immediate:true}) //不用使用immediate,默認(rèn)執(zhí)行一次 /*watchEffect(()=>{ fullName.value = person.firstName+"-"+person.lastName })*/ watchEffect(()=>{ const name = fullName.value.split('-'); person.firstName = name[0]; person.lastName = name[1]; }) return{ person, fullName } } }); </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
到此這篇關(guān)于Vue3中的 computed,watch,watchEffect的使用方法的文章就介紹到這了,更多相關(guān)Vue3 computed 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue el-upload單圖片上傳功能實(shí)現(xiàn)
這篇文章主要介紹了Vue el-upload單圖片上傳功能實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn)
這篇文章主要介紹了一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn),本文介紹在vue3的setup中使用composition?API獲取元素節(jié)點(diǎn)的幾種方法,需要的朋友可以參考一下2022-07-07vue watch內(nèi)部調(diào)用methods方法報(bào)錯(cuò)的解決方案
這篇文章主要介紹了vue watch內(nèi)部調(diào)用methods方法報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue項(xiàng)目配置eslint保存自動(dòng)格式化問題
這篇文章主要介紹了vue項(xiàng)目配置eslint保存自動(dòng)格式化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue實(shí)現(xiàn)首頁banner自動(dòng)輪播效果
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)首頁banner自動(dòng)輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue解決使用webpack打包后keep-alive不生效的方法
今天小編就為大家分享一篇vue解決使用webpack打包后keep-alive不生效的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09