当前位置: 首页 > 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.dtcms.com/a/310863.html

相关文章:

  • 深入 Go 底层原理(二):Channel 的实现剖析
  • 基于结构熵权-云模型的铸铁浴缸生产工艺安全评价
  • 打靶日记-RCE-labs(续)
  • linux eval命令的使用方法介绍
  • php完整处理word中表单数据的方法
  • 【软考中级网络工程师】知识点之级联
  • PHP面向对象编程与数据库操作完全指南-上
  • ctfshow_源码压缩包泄露
  • Arduino IDE离线安装ESP8266板管理工具
  • 网络安全基础知识【6】
  • Linux初步认识与指令与权限
  • 机器学习sklearn:聚类
  • 读书:李光耀回忆录-我一生的挑战-新加坡双语之路
  • 【物联网】基于树莓派的物联网开发【21】——MQTT获取树莓派传感器数据广播实战
  • Python So Easy 大虫小呓三部曲 - 高阶篇
  • html5+css3+canvas长文转长图工具支持换行
  • 国产嵌入式调试器之光? RT-Trace 初体验!
  • C++之vector类的代码及其逻辑详解 (中)
  • 电力系统分析学习笔记
  • 谷歌Chrome浏览器安装插件
  • 论文笔记:Bundle Recommendation and Generation with Graph Neural Networks
  • 设计Mock华为昇腾GPU的MindSpore和CANN的库的流程与实现
  • STM32——启动过程浅析
  • 个人电脑部署私有化大语言模型LLM
  • python+pyside6的简易画板
  • 损失函数和调度器相关类代码回顾理解 |nn.CrossEntropyLoss\CosineAnnealingLR
  • Codeforces Round 1040 (Div. 2) A - D题详细题解
  • DP-v2.1-mem-clean学习(3.6.8-3.6.8.1)
  • Java试题-选择题(3)
  • 风光储并离网切换仿真模型(下垂控制一次调频)