自定義指令分為全局和局部兩種類型,大家並不陌生。今天我們就講講在 vue3 中,自定義指令定義、使用以及鈎子函數都有哪些變化?
一、注冊自定義指令
以下實例都是實現一個輸入框自動獲取焦點的自定義指令。
1.1、全局自定義指令
在vue2中,全局自定義指令通過 directive 掛載到 Vue 對象上,使用 Vue.directive('name',opt)。
實例1:Vue2 全局自定義指令
Vue.directive('focus',{ inserted:(el)=>{ el.focus() } })
inserted 是鈎子函數,在綁定元素插入父節點時執行。
在 vue3 中,vue 實例通過createApp 創建,所以全局自定義指令的掛載方式也改變了, directive 被掛載到 app上。
實例2:Vue3 全局自定義指令
//全局自定義指令 app.directive('focus',{ mounted(el){ el.focus() } }) //組件使用 <input type="text" v-focus />
1.2、局部自定義指令
在組件內部,使用 directives 引入的叫做局部自定義指令。Vue2 和 Vue3 的自定義指令引入是一模一樣的。
實例3:局部自定義指令
<script> //局部自定義指令 const defineDir = { focus:{ mounted(el){ el.focus() } } } export default { directives:defineDir, setup(){} } </script>
二、自定義指令中的生命周期鈎子函數
一個指令定義對象可以提供如下幾個鈎子函數(都是可選的,根據需要引入)
- created :綁定元素屬性或事件監聽器被應用之前調用。該指令需要附加需要在普通的 v-on 事件監聽器前調用的事件監聽器時,這很有用。
- beforeMounted :當指令第一次綁定到元素並且在掛載父組件之前執行。
- mounted :綁定元素的父組件被掛載之后調用。
- beforeUpdate :在更新包含組件的 VNode 之前調用。
- updated :在包含組件的 VNode 及其子組件的 VNode 更新后調用。
- beforeUnmounted :在卸載綁定元素的父組件之前調用
- unmounted :當指令與元素解除綁定且父組件已卸載時,只調用一次。
實例3:測試指令內生命周期函數執行
<template> <div> <input type="text" v-focus v-if="show"><br> <button @click="changStatus">{{show?'隱藏':'顯示'}}</button> </div> </template> //局部自定義指令 const autoFocus = { focus:{ created(){ console.log('created'); }, beforeMount(){ console.log('beforeMount'); }, mounted(el){ console.log('mounted'); }, beforeUpdated(){ console.log('beforeUpdated') }, updated(){ console.log('updated'); }, beforeUnmount(){ console.log('beforeUnmount'); }, unmounted(){ console.log('unmounted'); } }, } import { ref } from 'vue' export default { directives:autoFocus, setup(){ const show = ref(true) return { show, changStatus(){ show.value = !show.value } } } }
通過點擊按鈕,我們發現創建 input 元素的時候,會觸發 created、beforeMount 和 mounted 三個鈎子函數。
隱藏 input 元素的時候,會觸發 beforeUnmount 和 unmounted 。
然而我們添加的 beforeUpdate 和 updated 函數並沒有執行。
此時我們把 input 元素上的 v-if 修改成 v-show 就會執行上述兩個方法了,具體的執行情況自行驗證下。
從 vue2 升級到 vue3 ,自定義指令的生命周期鈎子函數發生了改變,具體變化如下:
- bind 函數被替換成了beforeMounted。
- update 被移除。
- componentUpdated 被替換成了updated。
- unbind 被替換成了 unmounted。
- inserted 被移除。
三、自定義指令鈎子函數的參數
鈎子函數被賦予了以下參數:
- el:指令所綁定的元素,可以直接操作DOM。
- binding:是一個對象,包含該指令的所有信息。
binding 包含的屬性具體的分別為:
- arg 自定義指令的參數名。
- value 自定義指令綁定的值。
- oldValue 指令綁定的前一個值。
- dir 被執行的鈎子函數
- modifiers:一個包含修飾符的對象。
<template> <div> <div v-fixed >定位</div> </div> </template> <script> //自定義指令動態參數 const autoFocus = { fixed:{ beforeMount(el,binding){ console.log('el',el) console.log('binding',binding) } } } export default { directives:autoFocus, setup(){ } } </script>
四、自定義指令參數
自定義指令的也可以帶參數,參數可以是動態的,參數可以根據組件實例數據進行實時更新。
實例4:自定義指令動態參數
<template> <div> <div v-fixed:pos="posData" style="width:100px;height:100px;background:grey">定位</div> </div> </template> <script> //自定義指令動態參數 const autoFocus = { fixed:{ beforeMount(el,binding){ el.style.position = "fixed" el.style.left = binding.value.left+'px' el.style.top = binding.value.top + 'px' } } } export default { directives:autoFocus, setup(){ const posData = { left:20, top:200 } return { posData, } } } </script>
什么時候需要自定義指令?
- 需要對普通 DOM 元素進行底層操作,這時候就會用到自定義指令。
- 需要將某些功能在指定DOM元素上使用,但對於需要操作大量DOM元素或者大變動時候,推薦使用組件,而不是指令。