亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Vue3 Composition API的使用簡介

瀏覽:38日期:2022-10-01 11:19:33

vue3.0在7月發(fā)布了rc版本,vue-cli4.5后也支持選擇vue3作為備選版本可以體驗了,vue3的正式版本相必也不遠了。學不動了呀!!!!相比vue2.0版本(Option API),Composition API(組合API)算是3.0的重大變更之一了。

概述

Composition API 主要靈感來源于React Hooks,目的是通過一組低侵入式的、函數(shù)式的 API,使得我們能夠更靈活地「組合」組件的邏輯。

示例

<template> <div>{{count}}</div> <button @click='addCount'>添加</button></template><script lang='ts'>import { defineComponent, ref, onMounted } from ’vue’;export default defineComponent({ name: ’App’, setup () { const count = ref(0) const getCount = () => { count.value = Math.floor(Math.random() * 10) } const addCount = () => { count.value++ } onMounted(() => { getCount() }) return { count, addCount } }});</script>

Composition API顧名思義就是不再傳入data、mounted等參數(shù),通過引入的ref、onMounted等方法實現(xiàn)數(shù)據(jù)的雙向綁定、生命周期函數(shù)的執(zhí)行。

為什么需要

1.在組件比較復(fù)雜的情況下,可以將邏輯代碼合到一起去,而不會被option強行分隔。這提高了代碼質(zhì)量的上限,同時也拉低了代碼質(zhì)量的下限。來自官方的一張對比圖:

Vue3 Composition API的使用簡介

2.更好的進行復(fù)用。

在vue2中,想要復(fù)用部分邏輯的代碼,都是通過mixin進去。但mixin進去的內(nèi)容實際上很不直觀,而且相同命名會被覆蓋。而通過composition API,因為所有的方法都是引入的,可以將單獨某個邏輯進行封裝。例如對發(fā)送驗證碼倒計時功能進行封裝。

<template> <input type='number' placeholder='請輸入驗證碼'> <button v-if='count'>{{count}}秒后可重新發(fā)送</button> <button v-else @click='startCount'>發(fā)送驗證碼</button></template><script lang='ts'>import { defineComponent, ref, reactive } from ’vue’;const userCountDown = () => { const count = ref(0) const countDown = (num: number) => { count.value = num num-- if (num > 0) { setTimeout(() => { countDown(num) }, 1000) } } const startCount = () => { // get verifyCode countDown(60) } return { count, startCount }}export default defineComponent({ name: ’Home’, setup () { const { count, startCount } = userCountDown() return { count, startCount } }});</script>

3.更好的typescript支持。不會再往vue原型上添加很多內(nèi)容,而是通過引入的方式,類型定義會更清晰。

setup

setup是vue新增的一個選項,它是組件內(nèi)使用Composition API的入口。setup是在創(chuàng)建vue組件實例并完成props的初始化之后執(zhí)行。因為setup會在option api解析之前被調(diào)用,所以setup中的this會與options中得完全不一樣。為了避免混亂,在setup中不使用this。同時setup返回的值,可以在模板和其他option中使用。從設(shè)計上來說,vue官方是將所有的事情在setup里完成。setup返回值連接的是template模板與方法。

ref、reactive

既然不在傳入data,那么將數(shù)據(jù)創(chuàng)建和監(jiān)聽響應(yīng)式就需要通過vue暴露出來的功能 ref或reactive。兩者有所區(qū)別,ref用于基礎(chǔ)賦值類型的數(shù)據(jù),而reactive用于引用類型的數(shù)據(jù)。

其中基礎(chǔ)賦值類型的值,在setup方法中,需要用 .value的方式進行獲取和修改。因為賦值類型的值如果return出去返回值,就失去了數(shù)據(jù)的雙綁定。但是在template中,可以進行直接訪問。

<template> <div>{{count}} <button @click='changeCount'>添加</button> </div> <div>學生的姓名是:{{student.name}}</div> <div>學生的年齡是:{{student.age}} <button @click='changeStudentAge(20)'>添加</button> </div></template><script lang='ts'>import { defineComponent, ref, reactive } from ’vue’;export default defineComponent({ name: ’Home’, setup () { const count = ref(0) const changeCount = () => { count.value = count.value + 1 } const student = reactive({ name: ’Bob’, age: 12 }) const changeStudentAge = (age: number) => { student.age = age } return { count, changeCount, student, changeStudentAge } }});</script>computed與watch

<template> <div>{{count}}</div> <div>{{doubleCount}}</div> <button @click='addCount'>添加</button></template><script lang='ts'>import { defineComponent, ref, computed, watchEffect, watch } from ’vue’;export default defineComponent({ name: ’App’, setup () { const count = ref(0) watch(count, () => { // 如多個則用數(shù)組的方式傳入[count, count1] console.log(’watch’, count.value) }) watchEffect(() => { console.log(’watchEffect’, count.value) }) const addCount = () => { count.value++ } const doubleCount = computed(() => { return count.value * 2 }) return { count, doubleCount, addCount } }});</script>

watch與watchEffect的差別是,watchEffect會立馬執(zhí)行,執(zhí)行中被讀取的響應(yīng)式 數(shù)據(jù)會被觀測。而watch只有在watch對象有變化時才會執(zhí)行。

生命周期 beforeCreate -> 使用 setup() created -> 使用 setup() beforeMount -> onBeforeMount mounted -> onMounted beforeUpdate -> onBeforeUpdate updated -> onUpdated beforeDestroy -> onBeforeUnmount destroyed -> onUnmounted errorCaptured -> onErrorCaptured

以上就是Vue3 Composition API的使用簡介的詳細內(nèi)容,更多關(guān)于Vue3 Composition API的使用的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 品爱网在线观看视频免费视频 | 久青草国产手机在线观 | 成人免费视频一区 | 成年人的黄色 | 91视频丝袜 | www.91麻豆.com| 国产v欧美v日本v精品 | 久久午夜精品视频 | 久久只有这才是精品99 | 国产v国产v片大片线观看网站 | 欧美日韩在线成人 | 久久国产资源 | zsvdy午夜| 国产又黄又爽又色视频影视网免费 | 欧美毛片在线播放观看 | 青草国产精品久久久久久 | 欧美艳星性videose精品 | 欧美一级看片免费观看视频在线 | 91精品国产亚一区二区三区 | 午夜亚洲精品久久久久久 | 久久国产这里只精品免费 | 久草视频资源站 | 亚洲国产成人久久精品影视 | 美女拍拍拍爽爽爽爽爽爽 | 国产亚洲精品国产 | 国产露出调教91 | 精品福利视频在线观看视频 | 国产精品亚洲欧美大片在线看 | 色播99| jizjizjiz亚洲大全 | 欧美一级片毛片免费观看视频 | 亚洲精品一区二区三区 | 九九视频只有精品六 | 国产大片好看免费播放 | 成人a大片高清在线观看 | 三级毛片三级毛片 | 欧美天天射 | 男女在线观看啪网站 | 久久色播 | 91精品国产91久久久久福利 | 婷婷激情综合 |