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、兩個小"坑":
- 監(jiān)視 reactive 定義的響應(yīng)式數(shù)據(jù)時: oldValue 無法正確獲取、強(qiáng)制開啟了深度監(jiān)視(deep配置失效)
- 監(jiān)視 reactive 定義的響應(yīng)式數(shù)據(jù)中某個屬性時: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)視ref所定義的一個響應(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)視ref所定義的一個響應(yīng)式數(shù)據(jù)
watch(sum, (newValue, oldValue) => {
console.log("sum發(fā)生了變化", newValue, oldValue);
})
return {
sum
}
}
}
</script>
watch 還可以傳一個配置項,把 immediate 等配置傳進(jìn)去:
watch(sum, (newValue, oldValue) => {
console.log("sum發(fā)生了變化", newValue, oldValue);
},{immediate:true})2、情況二:當(dāng)有多個信息需要同時監(jiān)視時

<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)視ref所定義的多個響應(yīng)式數(shù)據(jù)
watch([sum,msg],(newValue, oldValue) => {
console.log("sum發(fā)生了變化", newValue, oldValue);
})
return {
sum,
msg
}
}
}
</script>3、情況三:監(jiān)視reactive所定義的一個響應(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)視reactive所定義的一個響應(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)視reactive所定義的一個響應(yīng)式數(shù)據(jù)某個屬性
//情況四:監(jiān)視reactive所定義的一個響應(yīng)式數(shù)據(jù)某個屬性
watch(()=>person.name, (newValue, oldValue) => {
console.log("person的name發(fā)生了變化", newValue, oldValue);
})
5、情況五:監(jiān)視 reactive 所定義的一個響應(yīng)式數(shù)據(jù)某些屬性
//情況五:監(jiān)視reactive所定義的一個響應(yīng)式數(shù)據(jù)某個屬性
watch([()=>person.name,()=>person.age], (newValue, oldValue) => {
console.log("person的name或age發(fā)生了變化", newValue, oldValue);
})
6、特殊情況,監(jiān)視對象中的某個對象屬性,要開始deep:true
watch(()=>person.job, (newValue, oldValue) => {
console.log("person的job發(fā)生了變化", newValue, oldValue);
},{deep:true})//由于監(jiān)視的是reactive對象中的某個屬性,deep奏效7、監(jiān)視 ref 定義的對象響應(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)視哪個屬性,監(jiān)視的回調(diào)中用到哪個屬性,那就監(jiān)視哪個屬性
watchEffect有點(diǎn)像computed:
。但computed注重的計算出來的值(回調(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>可以看到用到了哪個就監(jiān)視哪個

最后,我們使用 watch 和 watchEffect 實現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例
這篇文章主要介紹了Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
vue3接口數(shù)據(jù)賦值對象,渲染報錯問題及解決
這篇文章主要介紹了vue3接口數(shù)據(jù)賦值對象,渲染報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
VueTreeselect?參數(shù)options的數(shù)據(jù)轉(zhuǎn)換-參數(shù)normalizer解析
這篇文章主要介紹了VueTreeselect?參數(shù)options的數(shù)據(jù)轉(zhuǎn)換-參數(shù)normalizer解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
vue如何根據(jù)網(wǎng)站路由判斷頁面主題色詳解
這篇文章主要給大家介紹了關(guān)于vue如何根據(jù)網(wǎng)站路由判斷頁面主題色的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

