成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

VueUse中的這五個函數,也太好用了吧

開發 前端
VueUse 是 Anthony Fu 大佬的一個開源項目,它為Vue的開發者提供了大量用于 Vue2 和Vue3 的基本 Composition API 實用工具函數。

[[416807]]

VueUse 是 Anthony Fu 大佬的一個開源項目,它為Vue的開發者提供了大量用于 Vue2 和Vue3 的基本 Composition API 實用工具函數。

它有幾十個用于常見開發人員用例的解決方案,如跟蹤ref更改,檢測元素可見性,簡化常見Vue模式,鍵盤/鼠標輸入等。 這是真正節省開發時間的好方法,因為我們不必自己親手添加所有這些標準功能,拿來主義,用就對了(再次感謝大佬的付出)。

我喜歡VueUse庫,因為它在決定提供哪些實用工具時真正把開發者放在第一位,而且它是一個維護良好的庫,因為它與Vue的當前版本保持同步。

VueUse 有哪些實用方法?

如果你想看到每一個實用程序的完整列表,建議去看看官方文檔。但總結一下,VueUse 中有9種類型的函數。

  • Animation(動畫) - 包含易于使用的過渡、超時和計時功能
  • Browser (瀏覽器) - 可以用于不同的屏幕控件、剪貼板、首選項等等
  • Component (組件) - 為不同的組件方法提供簡寫
  • Sensors (傳感器)- 用來監聽不同的DOM事件、輸入事件和網絡事件
  • State (狀態) - 管理用戶狀態(全局,本地存儲,會話存儲)
  • Utility (實用方法)--不同的實用方法,如getters、conditionals、ref synchronization等。
  • Watch --更高級的觀察器類型,如可暫停的觀察器、放棄的觀察器和條件觀察器
  • 其它 - 事件、WebSockets和 Web workers 的不同類型的功能

將 Vueuse 安裝到 Vue 項目中

VueUse 的最大特點之一是,它只用一個包就能兼容 Vue2 和 Vue3!

安裝VueUse有兩種選擇:npm或 CDN

  1. npm i @vueuse/core # yarn add @vueuse/core 
  1. <script src="https://unpkg.com/@vueuse/shared"></script> 
  2. <script src="https://unpkg.com/@vueuse/core"></script> 

推薦使用NPM,因為它更容易理解,但如果我們使用CDN, 可能通過 window.VueUse 來訪問。

使用 npm,可以通過解構的方式來獲得想要的方法:

  1. import { useRefHistory } from '@vueuse/core' 

useRefHistory 跟蹤響應式數據的變化

useRefHistory跟蹤對 ref 所做的每一個改變,并將其存儲在一個數組中。這樣我們能夠輕松為應用程序提供撤銷和重做功能。

來看一個示例,在該示例中,我們做一個能夠撤銷的文本區域

第一步是在沒有 VueUse 的情況下創建我們的基本組件--使用ref、textarea、以及用于撤銷和重做的按鈕。

  1. <template> 
  2.   <p>  
  3.     <button> Undo </button> 
  4.     <button> Redo </button> 
  5.   </p> 
  6.   <textarea v-model="text"/> 
  7. </template> 
  8.  
  9. <script setup> 
  10. import { ref } from 'vue' 
  11. const text = ref(''
  12. </script> 
  13.  
  14. <style scoped> 
  15.   button { 
  16.     border: none; 
  17.     outline: none; 
  18.     margin-right: 10px; 
  19.     background-color: #2ecc71; 
  20.     color: white; 
  21.     padding: 5px 10px;; 
  22.   } 
  23. </style> 

接著,導入useRefHistory,然后通過 useRefHistory從 text 中提取history、undo和redo屬性。

  1. import { ref } from 'vue' 
  2. import { useRefHistory } from '@vueuse/core' 
  3.  
  4. const text = ref(''
  5. const { history, undo, redo } = useRefHistory(text) 

每當我們的ref發生變化,更新history屬性時,就會觸發一個監聽器。

為了看看底層做了什么,我們把 history 內容打印出來。并在單擊相應按鈕時調用 undo 和redo函數。

  1. <template> 
  2.   <p>  
  3.     <button @click="undo"> Undo </button> 
  4.     <button @click="redo"> Redo </button> 
  5.   </p> 
  6.   <textarea v-model="text"/> 
  7.   <ul> 
  8.     <li v-for="entry in history" :key="entry.timestamp"
  9.       {{ entry }} 
  10.     </li> 
  11.   </ul> 
  12. </template> 
  13.  
  14. <script setup> 
  15. import { ref } from 'vue' 
  16. import { useRefHistory } from '@vueuse/core' 
  17. const text = ref(''
  18. const { history, undo, redo } = useRefHistory(text) 
  19. </script> 
  20.  
  21. <style scoped> 
  22.   button { 
  23.     border: none; 
  24.     outline: none; 
  25.     margin-right: 10px; 
  26.     background-color: #2ecc71; 
  27.     color: white; 
  28.     padding: 5px 10px;; 
  29.   } 
  30. </style> 

直接,跑起來,效果如下:

VueUse中的這5個函數,也太好用了吧

還有不同的選項,為這個功能增加更多的功能。例如,我們可以深入追蹤 reactive 對象,并像這樣限制 history 記錄的數量。

  1. const { history, undo, redo } = useRefHistory(text, { 
  2.   deep: true
  3.   capacity: 10, 
  4. }) 

onClickOutside 關閉 modal

onClickOutside 檢測在一個元素之外的任何點擊。根據我的經驗,這個功能最常見的使用情況是關閉任何模態或彈出窗口。

通常,我們希望我們的模態屏蔽網頁的其余部分,以吸引用戶的注意和限制錯誤。然而,如果他們確實點擊了模態之外,我們希望它關閉。

要做到這一點,只有兩個步驟。

  • 為要檢測的元素創建一個模板引用
  • 使用這個模板ref運行onClickOutside

這是一個簡單的組件,使用onClickOutside彈出窗口。

  1. <template> 
  2.   <button @click="open = true"Open Popup </button> 
  3.   <div class="popup" v-if='open'
  4.     <div class="popup-content" ref="popup"
  5.       Lorem ipsum dolor sit amet consectetur adipisicing elit. Corporis aliquid autem reiciendis eius accusamus sequi, ipsam corrupti vel laboriosam necessitatibus sit natus vero sint ullam! Omnis commodi eos accusantium illum? 
  6.     </div> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script setup> 
  11. import { ref } from 'vue' 
  12. import { onClickOutside } from '@vueuse/core' 
  13. const open = ref(false) // state of our popup 
  14. const popup = ref() // template ref 
  15. // whenever our popup exists, and we click anything BUT it 
  16. onClickOutside(popup, () => { 
  17.   open.value  = false 
  18. }) 
  19. </script> 
  20.  
  21. <style scoped> 
  22.   button { 
  23.     border: none; 
  24.     outline: none; 
  25.     margin-right: 10px; 
  26.     background-color: #2ecc71; 
  27.     color: white; 
  28.     padding: 5px 10px;; 
  29.   } 
  30.   .popup { 
  31.     position: fixed; 
  32.     top: ; 
  33.     left: ; 
  34.     width: 100vw; 
  35.     height: 100vh; 
  36.     display: flex; 
  37.     align-items: center; 
  38.     justify-content: center; 
  39.     background: rgba(, , , 0.1); 
  40.   } 
  41.   .popup-content { 
  42.     min-width: 300px; 
  43.     padding: 20px; 
  44.     width: 30%; 
  45.     background: #fff; 
  46.   } 
  47. </style 

結果是這樣的,我們可以用我們的按鈕打開彈出窗口,然后在彈出內容窗口外單擊關閉它。

VueUse中的這5個函數,也太好用了吧

useVModel 簡化 v-model 的綁定。

Vue開發者的一個常見用例是為一個組件創建一個自定義的v-model綁定。這也要求我們的組件接受一個 value 作為 prop,每當這個 value 被修改,我們的組件就會向父類發出一個 update 事件。

useVModel函數將其簡化為只使用標準的ref語法。假設我們有一個自定義的文本輸入,試圖為其文本輸入的值創建一個v-model。通常情況下,我們必須接受一個 value的 prop,然后發出一個 change事件來更新父組件中的數據值。

我們可以使用useVModel,把它當作一個普通的ref,而不是使用ref并調用props.value和update:value。這有助于減少我們需要記住的不同語法的數量!

  1. <template> 
  2.     <div> 
  3.         <input  
  4.             type="text"  
  5.             :value="data" 
  6.             @input="update" 
  7.         /> 
  8.     </div> 
  9. </template> 
  10.  
  11. <script> 
  12. import { useVModel } from '@vueuse/core' 
  13. export default { 
  14.   props: ['data'], 
  15.   setup(props, { emit }) { 
  16.     const data = useVModel(props, 'data', emit) 
  17.     console.log(data.value) // equal to props.data 
  18.     data.value = 'name' // equal to emit('update:data''name'
  19.     const update = (event) => { 
  20.         data.value = event.target.value 
  21.     } 
  22.     return { 
  23.         data, 
  24.         update 
  25.     } 
  26.   }, 
  27. </script> 

每當需要訪問value時,我們只需調用.value,useVModel將從我們的組件 props 中給我們提供值。而每當改變對象的值時,useVModel 會向父組件發出一個更新事件。

下面是父組件的一個簡單示例

  1. <template> 
  2.   <div> 
  3.     <p> {{ data }} </p> 
  4.     <custom-input  
  5.       :data="data"  
  6.       @update:data="data = $event" 
  7.     /> 
  8.   </div> 
  9. </template> 
  10.  
  11. <script> 
  12. import CustomInput from './components/CustomInput.vue' 
  13. import { ref } from 'vue' 
  14. export default { 
  15.   components: { 
  16.     CustomInput, 
  17.   }, 
  18.   setup () { 
  19.     const data = ref('hello'
  20.     return { 
  21.       data 
  22.     } 
  23.   } 

運行結果如下,父方的值總是與子方的輸入保持同步:

VueUse中的這5個函數,也太好用了吧

使用 intersectionobserver 跟蹤元素的可見性

當確定兩個元素是否重疊時,useIntersectionObserver 是非常強大的。這方面的一個很好的用例是檢查一個元素在視口中是否當前可見。

基本上,它檢查目標元素與根元素/文檔相交的百分比。如果這個百分比超過了某個閾值,它就會調用一個回調,確定目標元素是否可見。

useIntersectionObserver提供了一個簡單的語法來使用IntersectionObserver API。我們所需要做的就是為我們想要檢查的元素提供一個模板ref。

默認情況下,IntersectionObserver將以文檔的視口為根基,閾值為0.1--所以當這個閾值在任何一個方向被越過時,我們的交集觀察器將被觸發。

示例:我們有一個假的段落,只是在我們的視口中占據了空間,目標元素,然后是一個打印語句,打印我們元素的可見性。

  1. <template> 
  2.   <p> Is target visible? {{ targetIsVisible }} </p> 
  3.   <div class="container"
  4.     <div class="target" ref="target"
  5.       <h1>Hello world</h1> 
  6.     </div> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script> 
  11. import { ref } from 'vue' 
  12. import { useIntersectionObserver } from '@vueuse/core' 
  13. export default { 
  14.   setup() { 
  15.     const target = ref(null
  16.     const targetIsVisible = ref(false
  17.     const { stop } = useIntersectionObserver( 
  18.       target, 
  19.       ([{ isIntersecting }], observerElement) => { 
  20.         targetIsVisible.value = isIntersecting 
  21.       }, 
  22.     ) 
  23.     return { 
  24.       target, 
  25.       targetIsVisible, 
  26.     } 
  27.   }, 
  28. </script> 
  29.  
  30. <style scoped> 
  31. .container { 
  32.   width: 80%; 
  33.   margin:  auto; 
  34.   background-color: #fafafa; 
  35.   max-height: 300px; 
  36.   overflow: scroll
  37. .target { 
  38.   margin-top: 500px; 
  39.   background-color: #1abc9c; 
  40.   color: white; 
  41.   padding: 20px; 
  42. </style> 

運行后,對應的值會更新:

VueUse中的這5個函數,也太好用了吧

我們還可以為我們的 Intersection Observer 指定更多的選項,比如改變它的根元素、邊距(計算交叉點時對根的邊界框的偏移)和閾值水平。

  1. const { stop } = useIntersectionObserver( 
  2.       target, 
  3.       ([{ isIntersecting }], observerElement) => { 
  4.         targetIsVisible.value = isIntersecting 
  5.       }, 
  6.       { 
  7.         // root, rootMargin, threshold, window 
  8.         // full options in the source: https://github.com/vueuse/vueuse/blob/main/packages/core/useIntersectionObserver/index.ts 
  9.         threshold: 0.5, 
  10.       } 

同樣重要的是,這個方法返回一個 stop 函數,我們可以調用這個函數來停止觀察交叉點。如果我們只想追蹤一個元素在屏幕上第一次可見的時候,這就特別有用。

在這段代碼中,一旦targetIsVisible被設置為true,observer 就會停止,即使我們滾動離開目標元素,我們的值也會保持為 true 。

  1. const { stop } = useIntersectionObserver( 
  2.       target, 
  3.       ([{ isIntersecting }], observerElement) => { 
  4.         targetIsVisible.value = isIntersecting 
  5.         if (isIntersecting) { 
  6.           stop() 
  7.         } 
  8.       }, 
  9.     ) 

使用 useTransition 做個數字加載動畫

useTransition是整個VueUse庫中我最喜歡的函數之一。它允許我們只用一行就能順利地在數值之間進行過渡。

如果使用 useTransition 做一個下面這樣的效果,要怎么做呢?

VueUse中的這5個函數,也太好用了吧

我們可以通過三個步驟來做到這一點。

  • 初始化一個 ref 變量 count ,初始值為 0
  • 使用 useTransition 創建一個變量 output
  • 改變 count 的值
  1. import { ref } from 'vue' 
  2. import { useTransition, TransitionPresets } from '@vueuse/core' 
  3.  
  4. const count = ref(0) 
  5.  
  6. const output = useTransition(count , { 
  7.   duration: 3000, 
  8.   transition: TransitionPresets.easeOutExpo, 
  9. }) 
  10.  
  11. count.value = 5000 
  12.  
  13. </script> 

然后在 template 中顯示 output 的值:

  1. <template> 
  2.   <h2>  
  3.     <p> Join over </p> 
  4.     <p> {{ Math.round(output) }}+ </p> 
  5.     <p>Developers </p> 
  6.   </h2> 
  7. </template> 
  8.  
  9. <script setup> 
  10. import { ref } from 'vue' 
  11. import { useTransition, TransitionPresets } from '@vueuse/core' 
  12. const count = ref(0) 
  13. const output = useTransition(count, { 
  14.   duration: 3000, 
  15.   transition: TransitionPresets.easeOutExpo, 
  16. }) 
  17. count.value = 5000 
  18. </script> 

運行結果:

VueUse中的這5個函數,也太好用了吧

我們還可以使用useTransition 轉換整個數字數組。 使用位置或顏色時,這非常有用。 使用顏色的一個很好的技巧是使用計算的屬性將RGB值格式化為正確的顏色語法。

  1. <template> 
  2.   <h2 :style="{ color: color } "> COLOR CHANGING </h2> 
  3. </template> 
  4.  
  5. <script setup> 
  6. import { ref, computed } from 'vue' 
  7. import { useTransition, TransitionPresets } from '@vueuse/core' 
  8. const source = ref([, , ]) 
  9. const output = useTransition(source, { 
  10.   duration: 3000, 
  11.   transition: TransitionPresets.easeOutExpo, 
  12. }) 
  13. const color = computed(() => { 
  14.   const [r, g, b] = output.value 
  15.   return `rgb(${r}, ${g}, ${b})` 
  16. }) 
  17. source.value = [255, , 255] 
  18. </script> 
VueUse中的這5個函數,也太好用了吧

總結

這不是VueUse的完整指南。這些只是我平常比較常用的函數,還有很多好用的函數,大家可以自行到官網去學習使用。

作者:Matt Maribojoc 譯者:前端小智 來源:medium

原文:https://learvue.co/2021/07/5-vueuse-library-functions-that-can-speed-up-development/

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2024-12-13 16:01:35

2022-05-31 09:42:49

工具編輯器

2020-06-18 10:02:25

Python 開發編程語言

2020-11-10 06:11:59

工具軟件代碼

2021-03-18 10:12:54

JavaCompletable字符串

2021-07-27 18:02:01

VueUse 函數開發

2022-07-14 08:36:28

NacosApollo長輪詢

2021-04-22 09:56:32

MYSQL開發數據庫

2022-08-01 07:02:06

SpringEasyExcel場景

2024-05-11 09:38:05

React編譯器React 19

2021-03-19 09:48:10

Jupyter Not插件Python

2020-06-23 15:58:42

心電圖

2020-12-29 10:45:55

開發設計代碼

2022-09-06 10:52:04

正則庫HumrePython

2021-09-10 10:15:24

Python人臉識別AI

2022-05-11 14:43:37

WindowsPython服務器

2022-07-25 06:42:24

分布式鎖Redis

2021-03-02 20:42:20

實戰策略

2022-06-28 07:14:23

WizTree磁盤文件清理

2023-03-31 08:22:48

javassitcglibAOP
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩av在线免费 | 国产偷自视频区视频 | 久久久www成人免费无遮挡大片 | 中文字幕国产第一页 | 日韩美香港a一级毛片免费 国产综合av | 国产丝袜一区二区三区免费视频 | 国产在线精品一区二区三区 | 一区二区国产精品 | 国产午夜精品久久久久免费视高清 | 国产精品久久久久久妇女6080 | 亚洲欧美在线视频 | 国产成人久久av免费高清密臂 | 成人精品在线视频 | 卡通动漫第一页 | 色综合天天天天做夜夜夜夜做 | 一级日批片| 国产综合av | 国产精品一区视频 | 日韩欧美一区在线 | 国产精品99久久久久久久vr | 日日噜噜噜夜夜爽爽狠狠视频97 | 国产精品不卡一区 | 欧美日韩高清 | 国产一区二区三区久久久久久久久 | 情侣酒店偷拍一区二区在线播放 | 午夜成人在线视频 | 国产区在线观看 | 国产精品久久久久久亚洲调教 | 91看片官网| 久久婷婷国产麻豆91 | 在线资源视频 | 欧美一级淫片007 | 免费一区| 一区免费看 | 黄色精品| 龙珠z在线观看 | 91精品国产综合久久婷婷香蕉 | 亚洲一区二区久久 | 国产精品成人久久久久 | 黑人精品欧美一区二区蜜桃 | 先锋资源吧 |