vue3中自定义指令使用

Vue 3 自定义指令详细使用指南

1. 基本结构

一个完整的自定义指令包含以下可选钩子函数:

javascript 复制代码
const myDirective = {
  // 指令第一次绑定到元素时调用(只调用一次)
  created(el, binding, vnode, prevVnode) {},
  
  // 元素插入父 DOM 前调用
  beforeMount(el, binding, vnode, prevVnode) {},
  
  // 元素插入父 DOM 后调用
  mounted(el, binding, vnode, prevVnode) {},
  
  // 父组件更新前调用
  beforeUpdate(el, binding, vnode, prevVnode) {},
  
  // 父组件和子组件都更新后调用
  updated(el, binding, vnode, prevVnode) {},
  
  // 父组件卸载前调用
  beforeUnmount(el, binding, vnode, prevVnode) {},
  
  // 父组件卸载后调用
  unmounted(el, binding, vnode, prevVnode) {}
}

2.钩子函数参数

3.注册方式

3.1全局注册

javascript 复制代码
// main.js
import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)

app.directive('focus', {
  mounted(el) {
    el.focus()
  }
})

app.mount('#app')

3.2 局部注册

javascript 复制代码
// 在组件中
export default {
  directives: {
    focus: {
      mounted(el) {
        el.focus()
      }
    }
  }
}

4.使用方式

5.实用示例

5.1权限控制指令

javascript 复制代码
app.directive('permission', {
  mounted(el, binding) {
    const userPermissions = ['edit', 'view'] // 通常从store获取
    if (!userPermissions.includes(binding.value)) {
      el.style.display = 'none'
      // 或者直接移除元素
      // el.parentNode.removeChild(el)
    }
  }
})

5.2 防抖点击指令

javascript 复制代码
app.directive('debounce-click', {
  mounted(el, binding) {
    const delay = binding.value || 500
    let timer = null
    
    el.addEventListener('click', () => {
      if (timer) clearTimeout(timer)
      timer = setTimeout(() => {
        binding.arg && binding.arg()
      }, delay)
    })
  },
  unmounted(el) {
    el.removeEventListener('click')
  }
})

5.3 拖拽指令

javascript 复制代码
app.directive('drag', {
  mounted(el) {
    let isDragging = false
    let offsetX, offsetY
    
    el.style.position = 'absolute'
    el.style.cursor = 'move'
    
    el.addEventListener('mousedown', (e) => {
      isDragging = true
      offsetX = e.clientX - el.getBoundingClientRect().left
      offsetY = e.clientY - el.getBoundingClientRect().top
    })
    
    document.addEventListener('mousemove', (e) => {
      if (!isDragging) return
      el.style.left = e.clientX - offsetX + 'px'
      el.style.top = e.clientY - offsetY + 'px'
    })
    
    document.addEventListener('mouseup', () => {
      isDragging = false
    })
  }
})

5.4 图片懒加载指令

javascript 复制代码
app.directive('lazy', {
  mounted(el, binding) {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          el.src = binding.value
          observer.unobserve(el)
        }
      })
    })
    observer.observe(el)
  }
})

6.注意事项

在 setup() 中定义的指令必须以 v 开头(如 vFocus),才能在模板中作为 v-focus 使用
指令必须从 setup() 中返回才能在模板中使用
对于需要清理的资源(如事件监听器),确保在 unmounted 钩子中进行清理
指令的 binding 参数是响应式的,但 el 不是
在 <script setup> 中定义的指令会自动暴露给模板,无需显式返回

相关推荐
李明一.16 分钟前
Java 全栈开发学习:从后端基石到前端灵动的成长之路
java·前端·学习
观默29 分钟前
我用AI造了个“懂我家娃”的育儿助手
前端·人工智能·产品
crary,记忆32 分钟前
微前端MFE:(React 与 Angular)框架之间的通信方式
前端·javascript·学习·react.js·angular
星空寻流年38 分钟前
javaScirpt学习第七章(数组)-第一部分
前端·javascript·学习
巴巴_羊2 小时前
React JSX语法
javascript·react.js·ecmascript
烛阴2 小时前
Python多进程开发实战:轻松突破GIL瓶颈
前端·python
爱分享的程序员2 小时前
前端面试专栏-主流框架:11. React Router路由原理与实践
前端·javascript·react.js·面试
刘 怼怼2 小时前
Element UI 表格中实现搜索关键字高亮的
vue.js·ui·elementui
weixin_459074352 小时前
在el-image组件的预览中添加打印功能(自定义功能)
前端·javascript·vue.js
知否技术2 小时前
2025微信小程序开发实战教程(三)
前端·微信小程序