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

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.lryc.cn/news/608037.html

相关文章:

  • 从毫秒到真义:构建工业级RAG系统的向量检索优化指南
  • 入门MicroPython+ESP32:ESP32烧录MicroPython固件
  • Python进阶(5):类与继承
  • Unity_数据持久化_XML存储相关
  • 探索:Uniapp 安卓热更新
  • 智能合约漏洞导致的损失,法律责任应如何分配
  • 医疗后台管理系统开发实践
  • 分类任务当中常见指标 F1分数、recall、准确率分别是什么含义
  • 通过解决docker network connect实现同一个宿主机不同网络的容器间通信
  • 【stm32】点灯及蜂鸣器
  • 深度学习·mmsegmentation基础教程
  • 前端开发(HTML,CSS,VUE,JS)从入门到精通!第三天(JavaScript)
  • ospf作业
  • 关于Web前端安全防御之点击劫持的原理及防御措施
  • winscp 连openwrt 返回127错误码
  • Java设计模式之行为型模式(解释器模式)实现方式举例说明
  • 大文件上传:自定义协议
  • 进程 Vs 线程
  • 电路原理图绘制专业实战教程1
  • 深入 Go 底层原理(十五):cgo 的工作机制与性能开销
  • Minimizing Coins(Dynamic Programming)
  • OAuth 2.0 的安全升级版授权协议 OAuth 2.1 详解
  • 【转】大模型安全治理的现状与展望
  • 【龙芯99派新世界】buildroot快速使用笔记
  • WPFC#超市管理系统(4)入库管理
  • STM32——启动过程浅析
  • Shell【脚本 02】离线安装配置Zookeeper及Kafka并添加service服务和开机启动(脚本分析)
  • Kubernetes Gateway API 详解:现代流量路由管理方案
  • Flink2.0学习笔记:Stream API 窗口
  • ubuntu 系统风扇控制软件 CoolerControl