溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

發(fā)布時(shí)間:2023-03-11 09:07:04 來源:億速云 閱讀:178 作者:iii 欄目:編程語言

這篇文章主要介紹了Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用文章都會有所收獲,下面我們一起來看看吧。

setup 語法糖

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

大家發(fā)現(xiàn)沒有,在我們前面幾篇文章中的案例代碼中,每個(gè)案例的模板中都有一些雷同代碼,這些代碼就是我們的setup函數(shù),但是作為組合API的入口函數(shù),我們所有的組合式API都要寫到里面,難道我們每次都要寫上這一坨么,其實(shí)在Vue中提供了setup 的語法糖,語法糖大家都知道是什么嘛?

就比如我們Vue2中的 v-model 不就是語法糖么,可以通過這樣一個(gè)指令省去了大量的雙向數(shù)據(jù)綁定的代碼!那我們來看一下我們的setup都夠簡化成為什么樣子,以下面代碼為例,我們聲明一個(gè)函數(shù),點(diǎn)擊按鈕觸發(fā)喊出打印 hi 這樣一個(gè)簡單的效果;

<template>
    <div>
        <button @click="hello">hello</button>
    </div>
</template>

<script>
export default {
    setup() {
        const hello = () => {
            console.log('hi')
        }

        return { hello }
    }
}
</script>
<template>
    <div>
        <button @click="hello">hello</button>
    </div>
</template>

<script setup>
const hello = () => {
    console.log('hi')
}
</script>

上面是我們使用setup語法糖后的代碼效果,功能實(shí)現(xiàn)上是一樣的;在 script setup 的標(biāo)簽中,所有的數(shù)據(jù)、函數(shù)可以直接在模板中使用!

在 script setup 中的頂層變量都可以直接在模板中使用

computed函數(shù)

computed 函數(shù)的使用:其實(shí)我們什么情況下會使用計(jì)算屬性呢,那一定是通過依賴的數(shù)據(jù)得到新的數(shù)據(jù)!

1)從Vue中引入computed
2)在setup中進(jìn)行使用,將一個(gè)函數(shù),函數(shù)的返回值就是計(jì)算好的數(shù)據(jù)
3)最后呢通過setup返回出去,模板進(jìn)行使用,如果使用setup語法糖后其實(shí)不需要這一步了

我們可以舉一個(gè)簡單的例子,比如我們定義一個(gè)成績數(shù)字,單純的分?jǐn)?shù)信息,那我們通過 computed 函數(shù)來為我們計(jì)算出超過60份的及格成績;我們就直接使用 script setup 的方式來編碼了哈!

<template>
    <div>
        <p>成績單</p>
        <a v-for="num in achievement"> {{ num }} / </a>
        <p>及格成績單</p>
        <a v-for="num in passList"> {{ num }} / </a>
    </div>
</template>

<script setup>
import { computed, ref } from 'vue';

const achievement = ref([44, 22, 66, 77, 99, 88, 70, 21])

const passList = computed(() => {
    return achievement.value.filter(item => item > 60)
})
</script>

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

watch 函數(shù)

跟computed函數(shù)一樣,watch函數(shù)也是組合式API中的一員,watch其實(shí)就是監(jiān)聽數(shù)據(jù)變化的函數(shù),那么在Vue3中它都有哪些用法呢?可以使用watch監(jiān)聽一個(gè)或者多個(gè)響應(yīng)式數(shù)據(jù),可以使用watch監(jiān)聽響應(yīng)式數(shù)據(jù)中的一個(gè)屬性(簡單數(shù)據(jù) or 復(fù)雜數(shù)據(jù))可以配置深度監(jiān)聽,也可以使用watch監(jiān)聽實(shí)現(xiàn)默認(rèn)執(zhí)行;我們來分開嘗試一下代碼的寫法

通過watch監(jiān)聽一個(gè)數(shù)據(jù)

watcha監(jiān)聽一個(gè)數(shù)據(jù),函數(shù)兩個(gè)參數(shù):第一個(gè)要監(jiān)聽的數(shù)據(jù),第二個(gè)參數(shù)是監(jiān)聽值發(fā)生變化后觸發(fā)的回調(diào)函數(shù),其中回調(diào)函數(shù)也有兩個(gè)參數(shù) 新值、老值

<template>
    <div>
        總贊數(shù):{{ num }} <button @click="num++">點(diǎn)贊</button>
    </div>
</template>

<script setup>
import { ref, watch } from 'vue';

//創(chuàng)建一個(gè)響應(yīng)式數(shù)據(jù),我們通過點(diǎn)贊按鈕改變num的值

const num = ref(0)
watch(num, (nv, ov) => {
    console.log(nv, ov)
})
</script>

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

通過watch監(jiān)聽多個(gè)數(shù)據(jù)

watcha監(jiān)聽多個(gè)數(shù)據(jù),例如下面的我們需要監(jiān)聽num和user對象的變化,函數(shù)兩個(gè)參數(shù):第一個(gè)要監(jiān)聽的數(shù)據(jù)(因?yàn)槭嵌鄠€(gè)數(shù)據(jù)所以用數(shù)組),第二個(gè)參數(shù)是監(jiān)聽值發(fā)生變化后觸發(fā)的回調(diào)函數(shù)。

<template>
    <div>
        總贊數(shù):{{ num }} <button @click="num++">點(diǎn)贊</button>
    </div>
    <p>姓名:{{ user.name }}</p>
    <p>年齡:{{ user.age }}</p>
    <button @click="user.age++">過年啦</button>
</template>

<script setup>
import { ref, watch, reactive } from 'vue';
const num = ref(0)
let user = reactive(
    {
        name: "幾何心涼",
        age: 18
    }
)
watch([num, user], () => {
    console.log('我監(jiān)聽到了')
})
</script>

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

通過watch監(jiān)聽對象的一個(gè)屬性(簡單類型)

watch監(jiān)聽對象的一個(gè)屬性并且是簡單類型的屬性,比如我們監(jiān)聽下面的user中的age值,他是一個(gè)簡單類型,那我們watch的第一個(gè)參數(shù)形式需要是將對象屬性作為返回值的函數(shù);第二個(gè)參數(shù)是改變后的回調(diào)函數(shù)。

<template>
    <p>姓名:{{ user.name }}</p>
    <p>年齡:{{ user.age }}</p>
    <button @click="user.age++">過年啦</button>
</template>

<script setup>
import { ref, watch, reactive } from 'vue';
let user = reactive(
    {
        name: "幾何心涼",
        age: 18
    }
)
watch(()=>user.age, () => {
    console.log('我監(jiān)聽到了user.age的變化')
})
</script>

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

通過watch監(jiān)聽對象的一個(gè)屬性(復(fù)雜類型)

watch監(jiān)聽對象的一個(gè)屬性并且是復(fù)雜類型的屬性,比如下面的我們要監(jiān)聽user中的info,我們嘗試一下改變user中info中的wages值,那我們watch的第一個(gè)參數(shù)形式需要是將對象屬性作為返回值的函數(shù);第二個(gè)參數(shù)是改變后的回調(diào)函數(shù)。這時(shí)候還需要第三個(gè)參數(shù)那就是 deep 開啟深度監(jiān)聽

<template>
    <p>姓名:{{ user.name }}</p>
    <p>年齡:{{ user.age }}</p>
    <p>薪資:{{ user.info.wages }}</p>
    <button @click="user.age++">過年啦</button>
    <button @click="user.info.wages+=2000">加薪了</button>
</template>

<script setup>
import { ref, watch, reactive } from 'vue';
let user = reactive(
    {
        name: "幾何心涼",
        age: 18,
        info:{
            wages:20000
        }
    }
)
watch(()=>user.info, () => {
    console.log('我監(jiān)聽到了user.info的變化')
},{
    deep:true
})
</script>

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

通過watch監(jiān)聽數(shù)據(jù)默認(rèn)執(zhí)行

其實(shí)這種情況并不多但是也會遇到這種情況,就是我們在監(jiān)聽數(shù)據(jù)變化的時(shí)候,先默認(rèn)執(zhí)行一次;其實(shí)就是添加我們的immediate參數(shù)為true,我們以最初的num為例哈!

<template>
    <div>
        總贊數(shù):{{ num }} <button @click="num++">點(diǎn)贊</button>
    </div>
</template>

<script setup>
import { ref, watch, reactive } from 'vue';
const num = ref(0)
watch(num, () => {
    console.log('我打印了')
},{
    immediate:true
})
</script>

Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用

關(guān)于“Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Vue3中的setup語法糖、computed函數(shù)、watch函數(shù)如何用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI