vue3【提效】使用 VueUse 高效開發(工具庫 @vueuse/core + 新增的組件庫 @vueuse/components)

Vueuse 是一個功能強大的 Vue.js 生態系統工具庫,提供了可重用的組件和函數,幫助開發者更輕松地構建復雜的應用程序。

官網 :https://vueuse.org/core/useWindowScroll/

安裝 VueUse

npm i @vueuse/core @vueuse/components

(可選)安裝自動導入,添加到 imports 中

      // 需自動導入方法的庫imports: ['vue','pinia','@vueuse/core','@vueuse/components']

工具庫

獲取鼠標坐標 useMouse()

在這里插入圖片描述

<script setup lang="ts">
const { x, y } = useMouse()
</script><template><div><p>x:{{ x }}</p><p>y:{{ y }}</p></div>
</template>

監聽鼠標按下 useMousePressed()

<script setup>
const { pressed } = useMousePressed()
</script><template><p>{{ pressed }}</p>
</template>

獲取鼠標選擇的文字 useTextSelection()

在這里插入圖片描述

<script setup>
const state = useTextSelection()
</script><template><p>一段供鼠標選擇的文字</p><p>被鼠標選擇的文字是:{{ state.text }}</p>
</template>

窗口滾動條 useWindowScroll()

// 獲取滾動條坐標
const { x, y } = useWindowScroll({ behavior: 'smooth' })
<!-- 滾動滾動條 -->
<button @click="x += 200">向右滾動 200 px</button>
<button @click="y += 200">向下滾動 200 px</button><!-- 滾動滾動條到指定位置 -->
<button @click="y = 600">向下滾動到 600 px</button>

元素滾動條 useScroll()

const el = ref<HTMLElement | null>(null)
const { x, y, isScrolling, arrivedState, directions } = useScroll(el)
<div ref="el" />

獲取窗口大小 useWindowSize()

const { width, height } = useWindowSize()

添加事件監聽 useEventListener

<script setup lang="ts">
let num = ref(0)
// 監聽鼠標移動
useEventListener('mousemove', () => {num.value++
})
</script><template><div class="p-20"><p>num:{{ num }}</p></div>
</template>
  • 組件卸載時,監聽事件會自動被移除

復制到剪貼板 useClipboard()

<script setup lang="ts">
const { copy, copied, isSupported, text } = useClipboard()let msg = '你好'async function doCopy() {copy(msg)if (copied) {alert('已復制到剪貼板!')}
}
</script><template><div class="p-20"><p>{{ msg }}</p><p v-if="text">已復制到剪貼板的內容:{{ text }}</p><button v-if="isSupported" @click="doCopy">復制</button></div>
</template>
  • copy 復制的方法
  • copied 是否完成復制
  • isSupported 瀏覽器是否支持復制到剪貼板
  • text 復制到剪貼板的內容

選擇本地文件 useFileDialog()

<script setup lang="ts">
const { files, open, reset, onChange } = useFileDialog()
onChange((files) => {console.log(files)
})
</script><template><button type="button" @click="open()">選擇文件</button><button type="button" :disabled="!files" @click="reset()">清空選擇</button><template v-if="files"><p>已選擇 <b>{{ `${files.length}` }}</b> 個文件</p><li v-for="file of files" :key="file.name">{{ file.name }}</li></template>
</template>

切換全屏模式 useFullscreen()

<script setup lang="ts">
const { isFullscreen, enter, exit, toggle } = useFullscreen()
</script><template><button v-if="isFullscreen" @click="exit">退出全屏</button><button v-else @click="enter">全屏</button><button @click="toggle">切換全屏模式</button>
</template>

圖片加載 useImage

<script setup>
const avatarUrl = 'https://place.dog/300/200'
const { isLoading, error } = useImage({ src: avatarUrl })
</script><template><span v-if="isLoading">圖片加載中...</span><span v-else-if="error">圖片加載失敗</span><img v-else :src="avatarUrl" />
</template>

獲取聯網信息 useNetwork()

const { isOnline, offlineAt, downlink, downlinkMax, effectiveType, saveData, type } = useNetwork()

判斷是否聯網 useOnline()

const online = useOnline()

給元素添加動畫 useOnline()

在這里插入圖片描述

<script setup>
const el = ref()
const {isSupported,animate,// actionsplay,pause,reverse,finish,cancel,// statespending,playState,replaceState,startTime,currentTime,timeline,playbackRate
} = useAnimate(el, { transform: 'rotate(360deg)' }, 1000)
</script><template><div class="p-40"><span ref="el" style="display: inline-block">旋轉360度</span></div>
</template>

可控的計時器 useIntervalFn()

<script setup>
let num = ref(0)
const { pause, resume, isActive } = useIntervalFn(() => {num.value++
}, 1000)
</script><template><div class="p-40"><div>{{ num }}</div><button v-if="isActive" @click="pause">暫停計時器</button><button v-else @click="resume">恢復計時器</button></div>
</template>

暫停代碼執行 promiseTimeout

import { promiseTimeout } from '@vueuse/core'
async function print() {// 開啟 console 的默認計時器console.time()// 打印當前 console默認計時器 的時間console.timeLog()// 等待1s后執行await promiseTimeout(1000)// 打印當前 console默認計時器 的時間console.timeLog()
}
print()

獲取網頁標題 useTitle()

const title = useTitle()
console.log(title.value) // 打印當前網頁的標題

更多工具可參考官網,持續更新中!

組件庫

圖片加載 UseImage

<script setup lang="ts">
import { UseImage } from '@vueuse/components'
</script><template><UseImage src="https://place.dog/300/200"><!-- 建議優化為圖片加載動畫 --><template #loading> 圖片加載中.. </template><template #error> 圖片加載失敗 </template></UseImage>
</template>

一鍵復制到剪貼板 UseClipboard

<script setup lang="ts">
import { UseClipboard } from '@vueuse/components'
</script><template><UseClipboard v-slot="{ copy, copied }" source="復制的內容"><button @click="copy()"><!-- 建議優化為復制相關的圖標 -->{{ copied ? '復制成功' : '復制' }}</button></UseClipboard>
</template>

獲取聯網狀態 UseNetwork / UseOnline

<script setup>
import { UseNetwork } from '@vueuse/components'
</script><template><UseNetwork v-slot="{ isOnline }"> 是否聯網: {{ isOnline }} </UseNetwork>
</template>

另一個也類似

<script setup>
import { UseOnline } from '@vueuse/components'
</script><template><UseOnline v-slot="{ isOnline }"> 是否聯網: {{ isOnline }} </UseOnline>
</template>

更多組件可參考官網,持續更新中!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/41173.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/41173.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/41173.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

llm學習-4(llm和langchain)

langchain說明文檔&#xff1a;langchain 0.2.6 — &#x1f99c;&#x1f517; langChain 0.2.6https://api.python.langchain.com/en/latest/langchain_api_reference.html#module-langchain.chat_models 1&#xff1a;模型 &#xff08;1&#xff09;自定義模型導入&#x…

跟《經濟學人》學英文:2024年07月06日這期 Amazon turns 30

As Amazon turns 30, three factors will define its next decade It will have to deal with trustbusters, catch up on AI and revive its core business 它將不得不應對反壟斷者&#xff0c;追趕人工智能并重振其核心業務 trustbuster&#xff1a; 美 [?tr?s(t)?b?s…

Java中的算法優化與復雜度分析

Java中的算法優化與復雜度分析 大家好&#xff0c;我是免費搭建查券返利機器人省錢賺傭金就用微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 在軟件開發中&#xff0c;算法的效率直接影響到程序的性能和響應速度。算法優化…

你真的會ELISA加樣嗎?

在ELISA實驗中&#xff0c;研究人員需要進行多次加樣步驟完成實驗操作。對于常規雙抗體夾心法ELISA&#xff0c;一般有如下加樣步聚&#xff0c;即加樣本、加檢測抗體、加酶結合物、加底物&#xff08;最后加終止液停止反應&#xff09;。 加樣步驟基礎知識 加樣步驟中一般使用…

云倉酒莊北京公司2024年:深耕酒業生態,以專業筑基

云倉酒莊北京公司&#xff1a;深耕酒業生態&#xff0c;以專業筑基&#xff0c;共繪酒業新藍圖 在競爭日益激烈的酒類市場中&#xff0c;云倉酒莊北京公司以其穩健的步伐、專業底蘊以及對品質的不懈追求&#xff0c;正逐步成為行業內一股不可忽視的力量。這家公司不僅僅是一個…

高級java每日一道面試題-2024年7月5日

題目&#xff1a; 請描述 Java 中接口和抽象類的區別&#xff0c;并說明什么時候應該使用接口&#xff0c;什么時候應該使用抽象類。 解答&#xff1a; 接口和抽象類都是 Java 中用于實現面向對象編程的重要概念&#xff0c;但它們在功能和用法上有一些區別&#xff1a; 1.…

把Windows打造成一個NTP網絡時間服務器,為網關提供校時服務

把Windows打造成一個NTP網絡時間服務器&#xff0c;為網關提供校時服務。主要目的是為了解決&#xff1a;當網關不能上外網的時候&#xff0c;可以使用局域網的電腦來當做NTP服務器&#xff0c;實現校時功能。 跟著小編來看&#xff0c;如何使用NTP網絡時間服務器來同步時間。 …

Laravel為什么會成為最優雅的PHP框架?

引言 在現代Web開發中&#xff0c;選擇一個合適的框架是構建高效、可靠和可維護應用的關鍵。從簡單的博客到復雜的企業級應用&#xff0c;PHP框架一直在不斷演變和進步。其中&#xff0c;Laravel作為一個相對較新的框架&#xff0c;自2011年首次發布以來&#xff0c;迅速崛起并…

ubuntu關于docker部署 項目一站式教程

**假設已有ubuntu服務器并且登錄root賬號 ** **FinalShell中復制快捷鍵是 ****Ctrl+Shift+V** 卸載老版本docker sudo apt-get remove docker docker-engine docker.io containerd runc安裝docker步驟 更新軟件包sudo apt update sudo apt upgrade安裝docker依賴sudo apt-get …

監控工具 Prometheus

監控工具 Prometheus Prometheus 是一個開源的監控解決方案&#xff0c;它能夠收集、存儲和查詢指標數據&#xff0c;并提供了強大的報警和可視化功能。Prometheus 適用于監控云原生應用程序和基礎設施&#xff0c;是 Kubernetes 生態系統中常用的監控工具之一。 1. Promethe…

Yarn有哪些功能特點

Yarn是一個由Facebook團隊開發&#xff0c;并聯合Google、Exponent和Tilde等公司推出的JavaScript包管理工具&#xff0c;旨在提供更優的包管理體驗&#xff0c;解決npm&#xff08;Node Package Manager&#xff09;的一些痛點。Yarn的功能特點主要包括以下幾個方面&#xff1…

分享 10個簡單實用的 JS 代碼技巧

代碼圖片生成工具&#xff1a;有碼高清 一、滾動到頁面頂部 我們可以使用 window.scrollTo() 平滑滾動到頁面頂部。 源碼&#xff1a; const scrollToTop () > {window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); };二、滾動到頁面底部 當然&…

漢王、繪王簽字版調用封裝

說明 需要配合漢王或繪王簽字版驅動以及對應的sdk服務使用 constants.js //漢王、繪王sdk websocket連接地址 export const WS_URLS {1:ws://127.0.0.1:29999, //漢王2:ws://127.0.0.1:7181, }export const COMMAND1 {1: {HWPenSign: "HWStartSign",nLogo: "…

探索大型語言模型自動評估 LLM 輸出長句準確性的方法

LLM現在能夠自動評估較長文本中的事實真實性 源碼地址&#xff1a;https://github.com/google-deepmind/long-form-factuality 論文地址&#xff1a;https://arxiv.org/pdf/2403.18802.pdf 這篇論文是關于谷歌DeepMind的&#xff0c;提出了新的數據集、評估方法和衡量標準&am…

vue2+element-ui新增編輯表格+刪除行

實現效果&#xff1a; 代碼實現 &#xff1a; <el-table :data"dataForm.updateData"border:header-cell-style"{text-align:center}":cell-style"{text-align:center}"><el-table-column label"選項字段"align"center&…

Linux 內核 GPIO 用戶空間接口

文章目錄 Linux 內核 GPIO 接口舊版本方式&#xff1a;sysfs 接口新版本方式&#xff1a;chardev 接口 gpiod 庫及其命令行gpiod 庫的命令行gpiod 庫函數的應用 GPIO&#xff08;General Purpose Input/Output&#xff0c;通用輸入/輸出接口&#xff09;&#xff0c;是微控制器…

MAX()和ROW_NUMBER()函數的對比

SQL 查詢中,使用 MAX() 函數和使用窗口函數 ROW_NUMBER() 都可以實現獲取每個分組中某個列的最大值,但它們的實現方式和性能表現有所不同。以下是兩者的區別和性能對比: 使用 MAX() 函數 SELECTMAX(d.times) FROMv_y d WHEREd.id = a.idAND d.name = a.nameAND d.age = a.…

交換機需要多大 buffer(續:更一般的原理)

前面用 aimd 系統分析了交換機 buffer 需求量隨流數量增加而減少&#xff0c;今天從更一般的角度繼續分析這事。 將交換機建模為一個 m/m/1 排隊系統&#xff0c;多流場景下它就會變成一個 m/g/1 排隊系統&#xff0c;而這事比前面的 aimd 系統分析更容易推導。 m/m/1 系統中…

哪里還可以申請免費一年期的SSL證書?

目前&#xff0c;要申請免費一年期的SSL證書&#xff0c;選項較為有限&#xff0c;因為多數供應商已轉向提供短期的免費證書&#xff0c;通常有效期為90天。不過&#xff0c;有一個例外是JoySSL&#xff0c;它仍然提供一年期的免費SSL證書&#xff0c;但是只針對教育版和政務版…

halcon學習

halcon列程詳細介紹-V1.3 從文件夾中遍歷圖片(用到的算子及實例) 1)list_files(::Directory,Options:Files) 功能:列出目錄中的所有文件 參數列表: 第1個參數Directory為輸入變量,需要列出的目錄名字,即輸入目錄地址; 第2個參數Options為輸入變量,默認值為files,建…