自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue3 Composition API

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

 示例

  1. <template> 
  2.   <div>{{count}}</div> 
  3.   <button @click="addCount">添加</button> 
  4. </template> 
  5.  
  6. <script lang="ts"
  7. import { defineComponent, ref, onMounted } from 'vue'
  8.  
  9. export default defineComponent({ 
  10.   name'App'
  11.   setup () { 
  12.     const count = ref(0) 
  13.     const getCount = () => { 
  14.       count.value = Math.floor(Math.random() * 10) 
  15.     } 
  16.     const addCount = () => { 
  17.       count.value++ 
  18.     } 
  19.     onMounted(() => { 
  20.       getCount() 
  21.     }) 
  22.  
  23.     return { 
  24.       count
  25.       addCount 
  26.     } 
  27.   } 
  28. }); 
  29. </script> 

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

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

2.更好的進行復用。

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

  1. <template> 
  2.   <input type="number" placeholder="請輸入驗證碼"
  3.   <button v-if="count">{{count}}秒后可重新發(fā)送</button> 
  4.   <button v-else @click="startCount">發(fā)送驗證碼</button> 
  5. </template> 
  6.  
  7. <script lang="ts"
  8. import { defineComponent, ref, reactive } from 'vue'
  9.  
  10. const userCountDown = () => { 
  11.   const count = ref(0) 
  12.   const countDown = (num: number) => { 
  13.     count.value = num 
  14.     num-- 
  15.     if (num > 0) { 
  16.       setTimeout(() => { 
  17.         countDown(num) 
  18.       }, 1000) 
  19.     } 
  20.   } 
  21.   const startCount = () => { 
  22.     // get verifyCode 
  23.     countDown(60) 
  24.   } 
  25.  
  26.   return { count, startCount } 
  27.  
  28. export default defineComponent({ 
  29.   name'Home'
  30.   setup () { 
  31.     const { count, startCount } = userCountDown() 
  32.     return { count, startCount } 
  33.   } 
  34. }); 
  35. </script> 

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

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

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

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

  1. <template> 
  2.   <div>{{count}} 
  3.     <button @click="changeCount">添加</button> 
  4.   </div> 
  5.   <div>學生的姓名是:{{student.name}}</div> 
  6.   <div>學生的年齡是:{{student.age}} 
  7.     <button @click="changeStudentAge(20)">添加</button> 
  8.   </div> 
  9. </template> 
  10.  
  11. <script lang="ts"
  12. import { defineComponent, ref, reactive } from 'vue'
  13.  
  14. export default defineComponent({ 
  15.   name'Home'
  16.   setup () { 
  17.     const count = ref(0) 
  18.     const changeCount = () => { 
  19.       count.value = count.value + 1 
  20.     } 
  21.     const student = reactive({ 
  22.       name'Bob'
  23.       age: 12 
  24.     }) 
  25.     const changeStudentAge = (age: number) => { 
  26.       student.age = age 
  27.     } 
  28.     return { 
  29.       count
  30.       changeCount, 
  31.       student, 
  32.       changeStudentAge 
  33.     } 
  34.   } 
  35. }); 
  36. </script> 

computed與watch

  1. <template> 
  2.   <div>{{count}}</div> 
  3.   <div>{{doubleCount}}</div> 
  4.   <button @click="addCount">添加</button> 
  5. </template> 
  6.  
  7. <script lang="ts"
  8. import { defineComponent, ref, computed, watchEffect, watch } from 'vue'
  9.  
  10. export default defineComponent({ 
  11.   name'App'
  12.   setup () { 
  13.     const count = ref(0) 
  14.     watch(count, () => { // 如多個則用數(shù)組的方式傳入[count, count1] 
  15.       console.log('watch'count.value) 
  16.     }) 
  17.     watchEffect(() => { 
  18.       console.log('watchEffect'count.value) 
  19.     }) 
  20.     const addCount = () => { 
  21.       count.value++ 
  22.     } 
  23.     const doubleCount = computed(() => { 
  24.       return count.value * 2 
  25.     }) 
  26.     return { 
  27.       count
  28.       doubleCount, 
  29.       addCount 
  30.     } 
  31.   } 
  32. }); 
  33. </script> 

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

生命周期

  1. beforeCreate -> 使用 setup() 
  2. created -> 使用 setup() 
  3. beforeMount -> onBeforeMount 
  4. mounted -> onMounted 
  5. beforeUpdate -> onBeforeUpdate 
  6. updated -> onUpdated 
  7. beforeDestroy -> onBeforeUnmount 
  8. destroyed -> onUnmounted 
  9. errorCaptured -> onErrorCaptured 

 

 

責任編輯:姜華 來源: 晨曦大前端
相關推薦

2024-03-01 11:32:22

Vue3APIVue.js

2021-08-11 08:31:42

前端技術Vue3

2021-12-16 08:27:54

Vue3 插件Vue應用

2021-12-01 08:11:44

Vue3 插件Vue應用

2025-03-26 10:29:22

Vue3前端API

2021-11-30 08:19:43

Vue3 插件Vue應用

2023-11-28 09:03:59

Vue.jsJavaScript

2021-07-29 12:05:18

Vue3Api前端

2022-06-13 08:39:21

Vue3API

2022-07-13 10:07:31

vue3組件監(jiān)聽器

2021-12-02 05:50:35

Vue3 插件Vue應用

2024-01-04 08:38:21

Vue3API慎用

2021-05-26 10:40:28

Vue3TypeScript前端

2021-11-16 08:50:29

Vue3 插件Vue應用

2022-03-10 11:04:04

Vue3Canvas前端

2022-07-11 10:01:23

Vue 3前端

2021-12-08 09:09:33

Vue 3 Computed Vue2

2021-09-26 00:24:58

開發(fā)項目TypeScript

2020-09-29 08:26:17

Vue3中新增的API

2024-11-06 10:16:22

點贊
收藏

51CTO技術棧公眾號