安全教育網(wǎng)站建設(shè)背景泰安今天最新招聘信息
鶴壁市浩天電氣有限公司
2026/01/24 10:39:30
安全教育網(wǎng)站建設(shè)背景,泰安今天最新招聘信息,網(wǎng)訊wx1860,工作室起名大全免費(fèi)取名vueUse/core 基礎(chǔ)與高階應(yīng)用指南 解鎖 Vue 3 組合式 API 的超級能力 ? 引言
在 Vue 3 組合式 API 生態(tài)中#xff0c;vueUse/core 無疑是一顆璀璨的明星。它提供了上百個精心設(shè)計(jì)的組合式函數(shù)#xff0c;覆蓋了從 DOM 操作到狀態(tài)管理、從網(wǎng)絡(luò)請求到瀏覽器 API 等幾乎所有前端…vueUse/core 基礎(chǔ)與高階應(yīng)用指南解鎖 Vue 3 組合式 API 的超級能力 ?引言在 Vue 3 組合式 API 生態(tài)中vueUse/core無疑是一顆璀璨的明星。它提供了上百個精心設(shè)計(jì)的組合式函數(shù)覆蓋了從 DOM 操作到狀態(tài)管理、從網(wǎng)絡(luò)請求到瀏覽器 API 等幾乎所有前端開發(fā)場景。本文將帶你從基礎(chǔ)到高階全面掌握vueUse/core的使用技巧讓你的 Vue 開發(fā)效率提升到新高度一、快速上手安裝# npmnpminstallvueuse/core# yarnyarnaddvueuse/core# pnpmpnpmaddvueuse/core基礎(chǔ)使用template div h1vueUse 示例/h1 p當(dāng)前時間: {{ time }}/p button clicktoggle開關(guān): {{ enabled }}/button /div /template script setup import { useNow, useToggle } from vueuse/core // 獲取當(dāng)前時間自動更新 const time useNow() // 創(chuàng)建一個開關(guān)狀態(tài) const [enabled, toggle] useToggle(false) /script二、核心功能分類1. 狀態(tài)管理useLocalStorage - 本地存儲狀態(tài)template div input v-modelname placeholder請輸入姓名 p你好{{ name }}/p /div /template script setup import { useLocalStorage } from vueuse/core // 狀態(tài)自動持久化到 localStorage const name useLocalStorage(user-name, Vue 開發(fā)者) /scriptuseSessionStorage - 會話存儲狀態(tài)import{useSessionStorage}fromvueuse/core// 僅在會話期間保持的狀態(tài)consttokenuseSessionStorage(auth-token,)2. 生命周期useMounted useUnmountedimport{useMounted,useUnmounted}fromvueuse/coreuseMounted((){console.log(組件已掛載)})useUnmounted((){console.log(組件已卸載)})useDebounceEffect - 防抖副作用template input v-modelsearchQuery placeholder搜索... /template script setup import { ref } from vue import { useDebounceEffect } from vueuse/core const searchQuery ref() // 300ms 防抖的搜索效果 useDebounceEffect( () { console.log(搜索:, searchQuery.value) // 執(zhí)行搜索請求 }, [searchQuery], { delay: 300 } ) /script3. DOM 操作useElementSize - 元素尺寸監(jiān)聽template div refel classresizable p寬度: {{ width }}px/p p高度: {{ height }}px/p /div /template script setup import { ref } from vue import { useElementSize } from vueuse/core const el ref(null) const { width, height } useElementSize(el) /script style scoped .resizable { resize: both; overflow: auto; border: 2px solid #ccc; padding: 20px; min-width: 200px; min-height: 200px; } /styleuseClipboard - 剪貼板操作template div input v-modeltext placeholder輸入要復(fù)制的內(nèi)容 button clickcopy復(fù)制/button p{{ copied ? 已復(fù)制 : }}/p /div /template script setup import { ref } from vue import { useClipboard } from vueuse/core const text ref(Hello, vueUse!) const { copy, copied } useClipboard() /script4. 網(wǎng)絡(luò)請求useFetch - 現(xiàn)代化的數(shù)據(jù)獲取template div button clickrefetch刷新數(shù)據(jù)/button div v-ifpending加載中.../div div v-else-iferror錯誤: {{ error.message }}/div div v-else h2{{ data?.title }}/h2 p{{ data?.body }}/p /div /div /template script setup import { useFetch } from vueuse/core const { data, pending, error, refetch } useFetch( https://jsonplaceholder.typicode.com/posts/1 ) /script5. 時間相關(guān)useCountdown - 倒計(jì)時器template div h2倒計(jì)時: {{ formattedTime }}/h2 button clickstart開始/button button clickpause暫停/button button clickreset重置/button /div /template script setup import { computed } from vue import { useCountdown } from vueuse/core // 設(shè)置倒計(jì)時30秒 const targetDate new Date(Date.now() 30000) const { days, hours, minutes, seconds, isActive, start, pause, reset } useCountdown( targetDate, { interval: 1000 } ) // 格式化顯示 const formattedTime computed(() ${minutes.value.toString().padStart(2, 0)}:${seconds.value.toString().padStart(2, 0)} ) /script三、高級應(yīng)用技巧1. 自定義組合式函數(shù)基于vueUse的工具函數(shù)我們可以構(gòu)建自己的組合式函數(shù)import{useLocalStorage,useDebounce,useEventListener}fromvueuse/coreimport{ref,computed}fromvue/** * 帶防抖的本地存儲搜索狀態(tài) */exportfunctionuseDebouncedSearch(initialValue,delay300){// 本地存儲的搜索詞constrawSearchuseLocalStorage(search-query,initialValue)// 防抖處理后的搜索詞constsearchuseDebounce(rawSearch,delay)// 清空搜索constclear(){rawSearch.value}// 搜索是否為空constisEmptycomputed(()!search.value)// 監(jiān)聽 Enter 鍵搜索useEventListener(document,keydown,(e){if(e.keyEnter){// 觸發(fā)搜索事件window.dispatchEvent(newCustomEvent(search,{detail:search.value}))}})return{search,rawSearch,clear,isEmpty}}使用自定義的組合式函數(shù)template input v-modelrawSearch placeholder搜索... button clickclear清空/button div v-if!isEmpty搜索結(jié)果: {{ search }}/div /template script setup import { useDebouncedSearch } from ./composables/useDebouncedSearch const { search, rawSearch, clear, isEmpty } useDebouncedSearch() /script2. 插件擴(kuò)展vueUse支持通過插件系統(tǒng)進(jìn)行擴(kuò)展// main.jsimport{createApp}fromvueimport{createVueUse}fromvueuse/coreimportAppfrom./App.vueconstappcreateApp(App)// 創(chuàng)建 vueUse 實(shí)例constvueUsecreateVueUse()// 注冊全局插件app.use(vueUse)app.mount(#app)3. 性能優(yōu)化useMemoize - 函數(shù)結(jié)果緩存import{useMemoize}fromvueuse/core// 計(jì)算密集型函數(shù)constexpensiveCalculation(a,b){console.log(執(zhí)行計(jì)算...)returnab}// 緩存計(jì)算結(jié)果constmemoizedCalculateuseMemoize(expensiveCalculation)// 第一次執(zhí)行輸出 執(zhí)行計(jì)算... 并返回結(jié)果console.log(memoizedCalculate(1,2))// 3// 第二次執(zhí)行直接返回緩存結(jié)果不輸出日志console.log(memoizedCalculate(1,2))// 3useThrottleFn - 函數(shù)節(jié)流import{useThrottleFn}fromvueuse/core// 節(jié)流處理的函數(shù)1秒內(nèi)最多執(zhí)行一次constthrottledFnuseThrottleFn((){console.log(執(zhí)行了節(jié)流函數(shù))// 執(zhí)行一些高頻操作},1000)// 連續(xù)調(diào)用只會每隔1秒執(zhí)行一次throttledFn()throttledFn()throttledFn()四、最佳實(shí)踐與注意事項(xiàng)1. 按需導(dǎo)入vueUse支持 tree-shaking建議按需導(dǎo)入以減小打包體積// 推薦按需導(dǎo)入import{useNow,useToggle}fromvueuse/core// 不推薦導(dǎo)入整個庫import*asVueUsefromvueuse/coreconst{useNow}VueUse2. 合理使用緩存對于網(wǎng)絡(luò)請求和計(jì)算密集型操作使用useFetch的緩存功能或useMemoize可以顯著提升性能const{data,refetch}useFetch(https://api.example.com/data,{cache:true,// 啟用緩存cacheTTL:60000// 緩存有效期1分鐘})3. 注意生命周期管理使用 DOM 相關(guān)的組合式函數(shù)時確保在組件掛載后使用// 推薦在組件掛載后使用 DOM 相關(guān)函數(shù)useMounted((){const{width,height}useElementSize(el)// ...})// 或者使用條件判斷if(el.value){const{width,height}useElementSize(el)// ...}五、總結(jié)vueUse/core為 Vue 3 開發(fā)者提供了一個強(qiáng)大而豐富的組合式函數(shù)庫它不僅能夠極大地提升開發(fā)效率還能幫助我們編寫更加優(yōu)雅、可維護(hù)的代碼。從基礎(chǔ)的 DOM 操作到復(fù)雜的狀態(tài)管理從簡單的時間處理到高級的性能優(yōu)化vueUse幾乎覆蓋了前端開發(fā)的方方面面。通過本文的學(xué)習(xí)相信你已經(jīng)對vueUse/core有了全面的了解。在實(shí)際項(xiàng)目中建議你根據(jù)需求選擇合適的組合式函數(shù)并結(jié)合本文介紹的高級技巧充分發(fā)揮vueUse的威力?最后別忘了持續(xù)關(guān)注vueUse的官方更新它的生態(tài)系統(tǒng)還在不斷壯大參考資料vueUse 官方文檔Vue 3 組合式 API 文檔MDN Web API 參考