Vue3自定義指令完全指南(附詳細(xì)代碼)
基本概念
在Vue3中,自定義指令是用于直接操作DOM的重要工具。相比Vue2,Vue3的指令系統(tǒng)進(jìn)行了優(yōu)化和簡(jiǎn)化。
生命周期鉤子
| 鉤子名稱 | 對(duì)應(yīng)Vue2名稱 | 觸發(fā)時(shí)機(jī) |
|---|---|---|
| created | bind | 元素屬性綁定前 |
| beforeMount | inserted | 元素插入父節(jié)點(diǎn)前 |
| mounted | - | 元素插入父節(jié)點(diǎn)后 |
| beforeUpdate | update | 組件更新前 |
| updated | componentUpdated | 組件及子組件更新后 |
| beforeUnmount | unbind | 元素卸載前 |
| unmounted | - | 元素卸載后 |
指令注冊(cè)方式
全局注冊(cè)
// main.js
const app = createApp(App)
app.directive('focus', {
mounted(el) {
el.focus()
}
})
局部注冊(cè)
export default {
directives: {
highlight: {
mounted(el, binding) {
el.style.backgroundColor = binding.value || 'yellow'
}
}
}
}
常用應(yīng)用場(chǎng)景
1. 自動(dòng)聚焦
// 使用
<input v-focus>
// 實(shí)現(xiàn)
app.directive('focus', {
mounted(el) {
el.focus()
}
})
2. 權(quán)限控制
// 使用
<button v-permission="['admin']">刪除</button>
// 實(shí)現(xiàn)
app.directive('permission', {
mounted(el, binding) {
const roles = ['user', 'editor', 'admin']
if (!binding.value.includes(roles.current)) {
el.parentNode.removeChild(el)
}
}
})
3. 防抖指令
// 使用
<button v-debounce:click="submitForm">提交</button>
// 實(shí)現(xiàn)
app.directive('debounce', {
mounted(el, binding) {
let timer = null
el.addEventListener(binding.arg, () => {
clearTimeout(timer)
timer = setTimeout(() => {
binding.value()
}, 500)
})
}
})
進(jìn)階技巧
動(dòng)態(tài)參數(shù)傳遞
// 使用
<div v-pin:[direction]="200"></div>
// 實(shí)現(xiàn)
app.directive('pin', {
mounted(el, binding) {
el.style.position = 'fixed'
const direction = binding.arg || 'top'
el.style[direction] = binding.value + 'px'
}
})
Composition API 結(jié)合
import { useScroll } from './scrollComposable'
app.directive('scroll', {
mounted(el, binding) {
const { start, stop } = useScroll(binding.value)
el._scrollHandler = { start, stop }
start()
},
unmounted(el) {
el._scrollHandler.stop()
}
})
注意事項(xiàng)
- 避免過度使用:優(yōu)先使用組件化方案
- 參數(shù)驗(yàn)證:使用
binding.value前進(jìn)行類型檢查 - 性能優(yōu)化:及時(shí)在unmounted階段清理副作用
- 命名規(guī)范:使用小寫字母+連字符格式(如
v-custom-directive) - 瀏覽器兼容:謹(jǐn)慎使用新API,必要時(shí)添加polyfill
總結(jié)
| 場(chǎng)景 | 推薦指令類型 | 示例 |
|---|---|---|
| DOM操作 | 自定義指令 | v-tooltip |
| 全局功能 | 全局指令 | v-permission |
| 復(fù)雜交互 | 組合式指令 | v-draggable |
| 簡(jiǎn)單樣式修改 | 類綁定 | :class=“…” |
通過合理使用自定義指令,可以使代碼更簡(jiǎn)潔、維護(hù)性更好,但要注意保持指令的單一職責(zé)原則。
到此這篇關(guān)于Vue3自定義指令的文章就介紹到這了,更多相關(guān)Vue3自定義指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3 微信掃碼登錄及獲取個(gè)人信息實(shí)現(xiàn)的三種方法
本文主要介紹了vue3 微信掃碼登錄及獲取個(gè)人信息實(shí)現(xiàn)的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Vue動(dòng)態(tài)組件表格的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue動(dòng)態(tài)組件表格的實(shí)現(xiàn)代碼,包括框架結(jié)構(gòu)組件,文中還給大家封裝了幾個(gè)組件,有按鈕組件、圖片組件、滑動(dòng)開關(guān),結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
vue從零實(shí)現(xiàn)一個(gè)消息通知組件的方法詳解
這篇文章主要介紹了vue從零實(shí)現(xiàn)一個(gè)消息通知組件的方法,結(jié)合實(shí)例形式分析了vue實(shí)現(xiàn)消息通知組件的具體原理、實(shí)現(xiàn)步驟、與相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
Vue2.0利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案
本篇文章主要介紹了Vue2 利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
vue3項(xiàng)目+element-plus:時(shí)間選擇器格式化方式
這篇文章主要介紹了vue3項(xiàng)目+element-plus:時(shí)間選擇器格式化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

