Vue3实战笔记(64)—Vue 3自定义指令的艺术:实战中的最佳实践


前言

书接上文,在Vue3中,自定义指令是一种强大的工具,允许我们扩展HTML元素的功能。通过自定义指令,我们可以创建可重用的行为,并将它们绑定到任何元素上。下面,本文备份一些简单的Vue3自定义指令超实用案例,并解释其实际应用场景。:


一、一些简单的Vue3自定义指令超实用案例

v-focus - 自动聚焦输入框

const app = Vue.createApp({
  data() {
    return {
      // ...
    }
  }
});

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

使用方式:

<input v-focus />

v-tooltip - 显示工具提示

app.directive('tooltip', {
  mounted(el, binding) {
    el.title = binding.value;
  },
  updated(el, binding) {
    el.title = binding.value;
  }
});

使用方式:

<span v-tooltip=" '这是一个提示信息' ">鼠标悬停</span>

v-click-outside - 点击元素外部时触发事件

app.directive('click-outside', {
  mounted(el, binding) {
    el.clickOutsideEvent = event => {
      if (!(el == event.target || el.contains(event.target))) {
        binding.value(event, el);
      }
    };
    document.body.addEventListener('click', el.clickOutsideEvent);
  },
  unmounted(el) {
    document.body.removeEventListener('click', el.clickOutsideEvent);
  }
});

使用方式:

<div v-click-outside="closeDropdown">
  <!-- ... -->
</div>

v-draggable - 使元素可拖拽

app.directive('draggable', {
  mounted(el) {
    el.style.cursor = 'move';
    el.style.position = 'fixed';
    let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
    el.onmousedown = dragMouseDown;

    function dragMouseDown(e) {
      e = e || window.event;
      e.preventDefault();
      pos3 = e.clientX;
      pos4 = e.clientY;
      document.onmouseup = closeDragElement;
      document.onmousemove = elementDrag;
    }

    function elementDrag(e) {
      e = e || window.event;
      e.preventDefault();
      pos1 = pos3 - e.clientX;
      pos2 = pos4 - e.clientY;
      pos3 = e.clientX;
      pos4 = e.clientY;
      el.style.top = (el.offsetTop - pos2) + 'px';
      el.style.left = (el.offsetLeft - pos1) + 'px';
    }

    function closeDragElement() {
      document.onmouseup = null;
      document.onmousemove = null;
    }
  }
});

使用方式:

<div v-draggable>
  拖拽我
</div>

v-scroll - 监听元素的滚动事件

app.directive('scroll', {
  mounted(el, binding) {
    el.addEventListener('scroll', binding.value);
  },
  unmounted(el, binding) {
    el.removeEventListener('scroll', binding.value);
  }
});

使用方式:

<div v-scroll="handleScroll">
  <!-- 内容 -->
</div>

这些自定义指令可以大大提高开发效率,帮助开发者实现一些常见且实用的功能。在实际开发中,你可以根据具体需求来定制自己的指令。


总结

在平凡的日子里,种下梦想的种子,静候花开。

相关推荐

  1. Vue3.2 定义指令详解与实战

    2024-07-10 20:24:05       57 阅读
  2. vue3 实现一个拖拽定义指令

    2024-07-10 20:24:05       37 阅读
  3. Vue2和Vue3定义指令写法

    2024-07-10 20:24:05       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 20:24:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 20:24:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 20:24:05       45 阅读
  4. Python语言-面向对象

    2024-07-10 20:24:05       55 阅读

热门阅读

  1. Postman与世界相连:集成第三方服务的全面指南

    2024-07-10 20:24:05       21 阅读
  2. 3033.修改矩阵

    2024-07-10 20:24:05       19 阅读
  3. 架构面试-数据库优化问题

    2024-07-10 20:24:05       19 阅读
  4. 精通Sklearn时间序列分析:预测未来的艺术

    2024-07-10 20:24:05       24 阅读
  5. OpenHarmony移植小型系统exynos4412(一)

    2024-07-10 20:24:05       19 阅读
  6. 适合selenium的防自动化检测的方法

    2024-07-10 20:24:05       21 阅读
  7. 使用Spring Boot和HBase实现大数据存储

    2024-07-10 20:24:05       19 阅读
  8. 华为机考真题 -- 篮球游戏

    2024-07-10 20:24:05       21 阅读