Vue3官方Hooks的用法與實現(xiàn)原理
Vue 3 引入了 Composition API,使得生命周期鉤子(hooks)在函數(shù)式風格中更清晰地表達。本篇文章將從官方 hooks 的使用、實現(xiàn)原理以及自定義 hooks 的結(jié)構(gòu)化思路出發(fā),全面理解 Vue 3 的 hooks 系統(tǒng)。
?? 1. Vue 3 官方生命周期 hooks 一覽
Hook 函數(shù) | 觸發(fā)時機 |
---|---|
onBeforeMount | 組件掛載前 |
onMounted | 組件掛載后 |
onBeforeUpdate | 數(shù)據(jù)變更、視圖更新前 |
onUpdated | 數(shù)據(jù)變更、視圖更新后 |
onBeforeUnmount | 卸載前 |
onUnmounted | 卸載后 |
onActivated | <KeepAlive> 激活時 |
onDeactivated | <KeepAlive> 失活時 |
onErrorCaptured | 錯誤捕獲 |
onRenderTracked | 響應(yīng)式依賴追蹤 |
onRenderTriggered | 響應(yīng)式依賴觸發(fā)更新 |
? 2. 使用示例
<script setup> import { onMounted } from 'vue' onMounted(() => { console.log('組件已掛載完成') }) </script>
在 <script setup> 中調(diào)用 hooks 更簡潔,無需訪問 setup() 返回對象。
?? 3. 實現(xiàn)原理解析
生命周期 hooks 的注冊基于當前組件實例 currentInstance
:
export function onMounted(hook: Function, target = currentInstance) { return injectHook('mounted', hook, target) }
核心函數(shù) injectHook
:
function injectHook(type: LifecycleHooks, hook: Function, target) { const hooks = target[type] || (target[type] = []) hooks.push(hook) }
當組件進入對應(yīng)生命周期階段時,Vue 內(nèi)部會遍歷執(zhí)行掛載在實例上的所有該類型 hook。
?? 4. 自定義 Hooks(組合邏輯復用)
Vue 3 鼓勵將邏輯拆分為 useXxx
函數(shù),以便跨組件復用。
?? 示例:useWindowSize
// useWindowSize.ts import { ref, onMounted, onUnmounted } from 'vue' export function useWindowSize() { const width = ref(window.innerWidth) const height = ref(window.innerHeight) const update = () => { width.value = window.innerWidth height.value = window.innerHeight } onMounted(() => window.addEventListener('resize', update)) onUnmounted(() => window.removeEventListener('resize', update)) return { width, height } }
使用方式:
<script setup> import { useWindowSize } from '@/hooks/useWindowSize' const { width, height } = useWindowSize() </script>
?? 自定義 hooks 的原理
- 本質(zhì)是普通函數(shù),內(nèi)部調(diào)用 Vue 組合式 API(如
ref
,watch
,onMounted
) - 在組件
setup()
時執(zhí)行,響應(yīng)式變量掛接到當前組件上下文 - 內(nèi)部調(diào)用生命周期鉤子時會依賴當前的
getCurrentInstance()
?? 5. 總結(jié)對比
類型 | 示例 | 作用 | 特點 |
---|---|---|---|
官方生命周期 Hook | onMounted() | 注冊生命周期回調(diào) | 由 Vue 內(nèi)部調(diào)度 |
自定義 Hook | useMousePosition() | 封裝響應(yīng)式狀態(tài) + 邏輯復用 | 是普通函數(shù),可組合嵌套 |
?? 推薦閱讀
?? 結(jié)語
生命周期 hooks 是 Vue 3 中非常核心的機制之一。理解它們的用法與實現(xiàn),有助于我們編寫結(jié)構(gòu)清晰、邏輯可復用的組件。
如果你對 Vue 源碼感興趣,不妨進一步探索 Vue 是如何依賴 currentInstance
來處理作用域鉤子的注冊和調(diào)用的。
到此這篇關(guān)于Vue3官方Hooks的用法與實現(xiàn)原理的文章就介紹到這了,更多相關(guān)Vue3 Hooks用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-table?樹形數(shù)據(jù)?tree-props?多層級使用避坑
本文主要介紹了el-table?樹形數(shù)據(jù)?tree-props?多層級使用避坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08關(guān)于vue?src路徑動態(tài)拼接的小知識
這篇文章主要介紹了vue?src路徑動態(tài)拼接的小知識,具有很好的參考價值,希望對大家有所幫助。2022-04-04vue3中關(guān)于路由hash與History的設(shè)置
這篇文章主要介紹了vue3中關(guān)于路由hash與History的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue實現(xiàn)echarts餅圖/柱狀圖點擊事件實例
echarts原生提供了相應(yīng)的API,只需要在配置好echarts之后綁定相應(yīng)的事件即可,下面這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)echarts餅圖/柱狀圖點擊事件的相關(guān)資料,需要的朋友可以參考下2023-06-06