当前位置: 首页 > backend >正文

Vue3+ts自定义指令

1. 在 directives/index.ts 中整合自定义指令:

在 src/directives/index.ts 文件中定义多个自定义指令。

// 防抖指令
interface DebounceBinding {(): void;time?: number;logPrefix?: string;
}export const debounce: Directive = {mounted(el: HTMLElement, binding: { arg: string; value: DebounceBinding }) {let timer: NodeJS.Timeout | null = null;el.addEventListener(binding.arg, () => {if (timer) {clearTimeout(timer);}const delay = binding.value.time || 300;const logPrefix = binding.value.logPrefix || 'Debounce';timer = setTimeout(() => {console.log(`${logPrefix}: 防抖后的函数执行`);binding.value();}, delay);});},unmounted(el: HTMLElement) {if (timer) {clearTimeout(timer);}}
};// 聚焦指令
export const focus: Directive = {inserted(el: HTMLElement) {el.focus();}
};// 图片懒加载指令
export const lazyload: Directive = {mounted(el: HTMLImageElement, binding: { value: string }) {const img = new Image();const loadingClass = 'image-loading';const errorClass = 'image-error';el.classList.add(loadingClass);const observer = new IntersectionObserver((entries, observer) => {entries.forEach(entry => {if (entry.isIntersecting) {img.src = binding.value;img.onload = () => {el.src = binding.value;el.classList.remove(loadingClass);observer.unobserve(el);};img.onerror = () => {el.classList.remove(loadingClass);el.classList.add(errorClass);observer.unobserve(el);};}});});observer.observe(el);}
};

2. 在 main.ts 中全局注册自定义指令:

import { createApp } from 'vue';
import App from './App.vue';
import { debounce, focus, lazyload } from './directives';const app = createApp(App);app.directive('debounce', debounce);
app.directive('focus', focus);
app.directive('lazyload', lazyload);app.mount('#app');

3. 在组件中使用自定义指令:

在 XXX.vue 中使用这几个自定义指令。

<template><div><input v-focus type="text" placeholder="自动聚焦"><input type="text" v-debounce:input="inputDebounce" :time="500" :logPrefix="自定义前缀">< img v-lazyload="imageUrl" alt="示例图片"></div>
</template><script setup lang="ts">
const inputDebounce = () => {console.log('实际执行的函数');
};const imageUrl = 'your-image-url.jpg';
</script>

在上述代码中,我们将 debounce 和 focus 两个自定义指令都定义在 directives/index.ts 文件中,然后在 main.ts 中进行全局注册,并在 App.vue 组件中使用它们。这样的结构使自定义指令的管理更加集中和清晰。

http://www.xdnf.cn/news/16846.html

相关文章:

  • Linux 动静态库的制作和使用
  • 笔记:C语言中指向指针的指针作用
  • DyWA:用于可推广的非抓握操作的动态自适应世界动作模型
  • 【高等数学】第七章 微分方程——第七节 常系数齐次线性微分方程
  • UniappDay07
  • 电力系统分析学习笔记(二)- 标幺值计算与变压器建模
  • 基于深度学习的医学图像分析:使用GAN实现医学图像增强
  • 重型机械作业误伤预警响应时间缩短80%!陌讯多模态识别算法在工程现场的应用优化
  • 安卓自动点击器:设置点击周期 / 滑动,抢票、游戏刷日常秒会
  • Unity_数据持久化_C#处理XML文件
  • 中科院自动化所机器人视觉中的多模态融合与视觉语言模型综述
  • 自进化智能体综述:通往人工超级智能之路
  • 第三十九章:AI导演的“魔法时间轴”:文生视频与Video Latent扩散结构
  • 论文阅读笔记:Dataset Condensation with Gradient Matching
  • 统信uos配置root账号并开启支持远程登录
  • WebSocket断线重连机制:保障实时通信的高可用性
  • 人工智能篇之计算机视觉
  • Kotlin协程极简教程:5分钟学完关键知识点
  • 2025牛客多校第六场 D.漂亮矩阵 K.最大gcd C.栈 L.最小括号串 个人题解
  • Vue3核心语法进阶(computed与监听)
  • True or False? 基于 BERT 学生数学问题误解检测
  • 垃圾收集器ParNewCMS与底层三色标记算法详解
  • 中州养老Day02:服务管理护理计划模块
  • 全球化 2.0 | 中国香港教育机构通过云轴科技ZStack实现VMware替代
  • 离线安装docker和docker-compose
  • UNet改进(28):KD Attention增强UNet的知识蒸馏方法详解
  • 【龙芯99派新世界】buildroot快速使用笔记
  • Makefile 入门与实践指南
  • 易华路副总经理兼交付管理中心部门经理于江平受邀PMO大会主持人
  • SQL Server从入门到项目实践(超值版)读书笔记 22