VueJS 之自定义指令

36 篇文章 2 订阅
16 篇文章 1 订阅
本文详细介绍了VueJS中的自定义指令,包括如何创建私有和全局自定义指令,以及它们在DOM操作中的应用。文章还探讨了指令的生命周期,如bind、update和unbind钩子函数,展示了如何在不同阶段操作元素。此外,还提到了简写形式以减少重复代码。
摘要由CSDN通过智能技术生成

参考

项目描述
搜索引擎Bing
哔哩哔哩黑马程序员
VueJS 官方教程自定义指令
VueJS 深度指南(官方)自定义指令

描述

项目描述
Edge109.0.1518.70 (正式版本) (64 位)
操作系统Windows 10 专业版
@vue/cli5.0.8
npm8.19.3
VueJS2.6.14

自定义指令

VueJS 允许你注册自定义指令,以便封装对 DOM 元素的重复处理行为,提高代码的复用率。

VueJS 中的自定义指令分为两类,即全局自定义指令及私有自定义指令。
其中,私有自定义指令仅允许当前组件(定义该指令的组件)使用,而全局自定义指令允许当前项目中的任何组件使用。

私有自定义指令

你可以通过 exprot default {} 终端 directives 节点来定义私有自定义指令。

<template>
  <div class="container">
    <!-- 为 .box 元素绑定自定义指令 color -->
    <div class="box" v-color></div>
  </div>
</template>

<script>
export default {
  // 定义私有自定义指令
  directives: {
    // 定义自定义指令 color
    color: {
      // 当元素绑定指令后将立即调用 bind 函数
      bind(el) {
        // 生成随机 RGB 颜色
        const r = Math.floor(Math.random() * 256);
        const g = Math.floor(Math.random() * 256);
        const b = Math.floor(Math.random() * 256);
        // 为绑定该指令的元素更换颜色
        el.style.backgroundColor = 'rgb(' + r + ' ,' + g + ' ,' + b + ')'
      }
    }
  }
}
</script>

<style scoped>
  .box{
    width: 150px;
    height: 150px;
  }
</style>

执行效果:

每次刷新该页面,.box 元素的背景颜色都将随机生成。

效果

注:

  1. 当你定义的自定义指令的名称为 color 时,如果你需要为某个元素绑定该指令,那么你需要为指令添加前缀 v-
  2. 提交给自定义指令的对象中存在 bind() 函数,你可以向该函数提供一个形参用以接收绑定该指令的元素对应的 DOM 对象。

全局自定义指令

你可以使用 Vue.directive() 函数定义全局自定义指令,该函数接收两个实参,第一个参数用以指定指令的名称,第二个参数用以为该指令提供配置对象。

举个栗子:

请向 Vue 项目 中的 main.js 文件中添加如下内容

// 定义全局自定义指令
Vue.directive('color', {
  // 当元素绑定指令后将立即调用 bind 函数
  bind(el) {
    // 生成随机 RGB 色值
    const r = Math.floor(Math.random() * 256);
    const g = Math.floor(Math.random() * 256);
    const b = Math.floor(Math.random() * 256);
    // 为绑定该指令的元素更换颜色
    el.style.backgroundColor = 'rgb(' + r + ' ,' + g + ' ,' + b + ')';
    }
  }
)

App.vue

<template>
  <div class="container">
    <!-- 为 .box 元素绑定自定义指令 color -->
    <div class="box" v-color></div>
  </div>
</template>

<script>
export default {

}
</script>

<style scoped>
  .box{
    width: 150px;
    height: 150px;
  }
</style>

执行效果:

每次刷新该页面,.box 元素的背景颜色都将随机生成。

效果

细节

指令周期

钩子

你可以在定义自定义属性时所用到的配置对象中使用如下钩子函数,这些钩子函数将在特定的时机被执行。

项目描述
bind该钩子函数仅在指令第一次绑定元素的时候被调用。
update当指令绑定的数据发生变化时都将触发该函数。
unbind该钩子函数仅在元素第一次解绑元素时被调用。

注:

你可以向上述钩子函数提供两个形参,第一个形参用于接收绑定该指令的 DOM 元素,而第二个形参用于接收该指令对应的实例对象。

指令的实例对象
<template>
  <div class="container">
    <!-- 你可以为指令绑定值 -->
    <div class="box" v-color:Param="'dodgerblue'"></div>
  </div>
</template>

<script>
export default {
  // 定义私有自定义指令
  directives: {
    // 定义自定义指令 color
    color: {
      bind(el, binding) {
        // 将指令对应的实例对象打印在控制台中
        console.log(binding);
      }
    }
  }
}
</script>

<style scoped>
  .box{
    width: 150px;
    height: 150px;
  }
</style>

执行效果:

在执行上述代码后,你将在浏览器中的控制台中观察到如下内容:

效果

其中:

项目描述
arg传递给指令的参数。
expression传递给指令的 JavaScript 表达式。
value传递给指令的表达式的执行结果。
modifiers一个包含修饰符的对象 。例如在 v-my-directive.foo.bar 中,修饰符对象是 { foo: true, bar: true }

update 钩子函数

当指令绑定的数据发生变化时都将触发 update 钩子函数。

举个栗子

<template>
  <div class="container">
    <!-- 为 .box 元素绑定 color 指令并为该指令绑定数据 -->
    <div class="box" v-color="color"></div>
    <!-- 为按钮绑定点击事件 -->
    <button @click="randomColor()">Random</button>
  </div>
</template>

<script>
export default {
  // 定义数据
  data() {
    return {
      // 定义 .box 元素的初始颜色
      color: 'dodgerblue'
    }
  },
  // 定义指令
  directives: {
    color: {
      bind(el, binding) {
        // binding 即该指令所绑定的数据
        el.style.backgroundColor = binding.value;
      },
      // 在当前指令绑定的数据发生变化时执行 update 钩子函数
      update(el, binding) {
        el.style.backgroundColor = binding.value;
        // 在控制台中打印当前指令的实例对象
        console.log(binding);
      }
    }
  },
  // 定义事件
  methods: {
    randomColor() {
      // 生成随机 RGB 色值
      const r = Math.floor(Math.random() * 256);
      const g = Math.floor(Math.random() * 256);
      const b = Math.floor(Math.random() * 256);      
      this.color = 'rgb(' + r + ' ,' + g + ' ,' + b + ')';
    }
  }
}
</script>

<style scoped>
  .box{
    width: 150px;
    height: 150px;
    border-radius: 3px;
  }
</style>

执行效果:

效果

注:

使用 update 周期函数时,指针的实例对象将增加 oldValue 属性,用于表示指令发生变化前传递给指令的表达式的结果值。

效果

简写

bindupdate 函数中的代码逻辑相同时,我们可以将对象格式的自定义指令转换为函数格式的对象指令。

举个栗子

<template>
  <div class="container">
    <!-- 为 .box 元素绑定 color 指令并为该指令绑定数据 -->
    <div class="box" v-color="color"></div>
    <!-- 为按钮绑定点击事件 -->
    <button @click="randomColor()">Random</button>
  </div>
</template>

<script>
export default {
  // 定义数据
  data() {
    return {
      // 定义 .box 元素的初始颜色
      color: 'dodgerblue'
    }
  },
  // 定义指令
  directives: {
    color(el, binding) {
      // binding 即该指令所绑定的数据
      el.style.backgroundColor = binding.value;
      console.log(binding.oldValue)
    }
  },
  // 定义事件
  methods: {
    randomColor() {
      // 生成随机 RGB 色值
      const r = Math.floor(Math.random() * 256);
      const g = Math.floor(Math.random() * 256);
      const b = Math.floor(Math.random() * 256);      
      this.color = 'rgb(' + r + ' ,' + g + ' ,' + b + ')';
    }
  }
}
</script>

<style scoped>
  .box{
    width: 150px;
    height: 150px;
    border-radius: 3px;
  }
</style>

上述代码的执行效果与上个示例的执行效果相同。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BinaryMoon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值