Vue3 自定义指令

Vue 自带的指令常用的有:v-if、v-for、v-model、v-show等,下面主要了解一下vue的自定义指令。

指令钩子

一个指令的定义对象可以提供几种钩子函数 (都是可选的)

const myDirective = {
  // 在绑定元素的 attribute 前
  // 或事件监听器应用前调用
  created(el, binding, vnode) {
    // 下面会介绍各个参数的细节
  },
  // 在元素被插入到 DOM 前调用
  beforeMount(el, binding, vnode) {},
  // 在绑定元素的父组件
  // 及他自己的所有子节点都挂载完成后调用
  mounted(el, binding, vnode) {},
  // 绑定元素的父组件更新前调用
  beforeUpdate(el, binding, vnode, prevVnode) {},
  // 在绑定元素的父组件
  // 及他自己的所有子节点都更新后调用
  updated(el, binding, vnode, prevVnode) {},
  // 绑定元素的父组件卸载前调用
  beforeUnmount(el, binding, vnode) {},
  // 绑定元素的父组件卸载后调用
  unmounted(el, binding, vnode) {}
}

钩子参数详解

  • el:指令绑定到的元素。这可以用于直接操作 DOM。

  • binding:一个对象,包含以下属性。

    • value:传递给指令的值。例如在 v-my-directive="1 + 1" 中,值是 2
    • oldValue:之前的值,仅在 beforeUpdate 和 updated 中可用。无论值是否更改,它都可用。
    • arg:传递给指令的参数 (如果有的话)。例如在 v-my-directive:foo 中,参数是 "foo"
    • modifiers:一个包含修饰符的对象 (如果有的话)。例如在 v-my-directive.foo.bar 中,修饰符对象是 { foo: true, bar: true }
    • instance:使用该指令的组件实例。
    • dir:指令的定义对象。
  • vnode:代表绑定元素的底层 VNode。

  • prevVnode:代表之前的渲染中指令所绑定元素的 VNode。仅在 beforeUpdate 和 updated 钩子中可用。

<script setup> 中定义使用

在 <script setup> 中,任何以 v 开头的驼峰式命名的变量都可以被用作一个自定义指令。如下:

<script setup>
// 在模板中启用 v-focus
const vFocus = {
  mounted: (el) => el.focus()
}
</script>

<template>
  <input v-focus />
</template>

该指令比 autofocus attribute 更有用,因为它不仅仅可以在页面加载完成后生效,还可以在 Vue 动态插入元素后生效。

不在<script setup> 中使用

在没有使用 <script setup> 的情况下,自定义指令需要通过 directives 选项注册:

// 局部自定义指令
export default {
  setup() {
    /*...*/
  },
  directives: {
    // 在模板中启用 v-focus
    focus: {
      /* ... */
    }
  }
}
// 全局自定义指令
const app = createApp({})

// 使 v-focus 在所有组件中都可用
app.directive('focus', {
  /* ... */
})

动态参数指令 

<div v-example:[arg]="value"></div>

这里指令的参数会基于组件的 arg 数据属性响应式地更新。

函数简写形式

对于自定义指令来说,如果仅需要在 mounted 和 updated 上实现相同的行为,除此之外并不需要其他钩子。这种情况下我们可以直接用一个函数来定义指令,如下:

<script setup lang="ts">
  import type { Directive } from 'vue';
  // 后台返回的权限数据
  const permission=[
    'created',
  ]
  // 自定义权限指令
  const vHasPermission:Directive<HTMLElement,string>=(el,bingding)=>{
    if(!permission.includes(bingding.value)){
      el.style.display='none'
    }
  }
  // 自定义移动指令
  const vMove:Directive=(el:HTMLElement)=>{
    const firstElementChild=el.firstElementChild as HTMLDivElement
    const mouseDown=(e:MouseEvent)=>{
      const currentX=e.offsetX
      const currentY=e.offsetY
      const mousemove=(e:MouseEvent)=>{
        el.style.left=e.clientX-currentX+'px'
        el.style.top=e.clientY-currentY+'px'
      }
      document.addEventListener('mousemove',mousemove)
      document.addEventListener('mouseup',()=>{
        document.removeEventListener('mousemove',mousemove)
      })
    }
    firstElementChild.addEventListener('mousedown',mouseDown)
  }
</script>

<template>
  <button v-has-permission="'created'">创建</button>
  <button v-has-permission="'edit'">编辑</button>
  <div class="container" v-move>
    <div class="header">header</div>
    <div>内容</div>
  </div>
</template>
<style scoped>
.container{
  position: fixed;
  top:100px;
  left: 100px;
  width: 50vw;
  border: 1px solid #ccc;
  height: 50vh;
}
.header{
  padding: 5px;
  background: #ccc;
}
</style>

注意:只有当所需功能只能通过直接的 DOM 操作来实现时,才应该使用自定义指令。其他情况下应该尽可能地使用 v-bind 这样的内置指令来声明式地使用模板,这样更高效,也对服务端渲染更友好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值