Vue3自定义指令解析,快拿去收藏使用一波~😎

扯皮

现在有很多自定义指令封装好可实现的功能,都能从hooks函数中找到,例如大名鼎鼎的VueUse,大部分功能都有。

至于为什么写这篇文章呢,本着复盘记录vue技术点加上业务当中涉及到的一部分需求(要是用到第三方插件岂不是大材小用了),主要还是复盘一波理论上的内容,便于mianxi~(懂得都懂),于是乎就写下一篇文章作为经验分享 个人参考使用~

构思

简简单单就拿常见的【复制】功能来说,利用到vue的特性是非常棒的。

举个栗子:假设我们需要展示的一些数据或者链接来说,根据用户角度考虑,一定是体验度越高充分表明这家业务水平做的优秀~

展示

介于我用的是Vue3 Ts Vite Element-Plus的技术栈,运用到尤大极力推荐的语法糖写法(嘎嘎香),

在根目录下创建directives指令文件夹,声明directive.ts作为全局指令存储文件,

为什么这么做呢?因为鉴于到后期的维护性能来说,

放在全局较为妥当,当然!局部的指令也可以放在这里噢~ 【具体需求具体使用】

main.ts 挂载全局指令

TypeScript 复制代码
import myDirective from "@/directives/directive"
myDirective(app)
app.mount("#app");

directive.ts 全局指令

TypeScript 复制代码
const myDirective = (app: any) => {
    app.directive("copy", {
        mounted: (el: any, binding:any, {value}: { value: string },) => {
            el.value = value
            el.handler = () => {
                if (!el.value) return ElMessage({message: "Nothing to copy!", type: "warning"})
                if (navigator.clipboard && window.isSecureContext) {
                    navigator.clipboard.writeText(el.value)
                        .then(() => {
                            return ElMessage({message: "Copy successfully!", type: "success"})
                        })
                        .catch(() => {
                            return ElMessage({message: "Replication failed!", type: "error"})
                        })
                } else {
                    let textarea = document.createElement('textarea')
                    textarea.readOnly = true
                    textarea.style.position = 'absolute'
                    textarea.style.left = '-9999px'
                    textarea.value = el.value
                    document.body.appendChild(textarea)
                    textarea.select()
                    const result = document.execCommand('Copy')
                    if (result) ElMessage({message: "Copy successfully!", type: "success"})
                    else ElMessage({message: "Replication failed!", type: "error"})
                    document.body.removeChild(textarea)
                }
            }
            el.addEventListener('click', el.handler)
        },
        updated: (el: any, {value}: { value: string }) => el.value = value,
        unmounted: (el: any) => el.removeEventListener('click', el.handler)
    })
}

demo.vue 使用指令

html 复制代码
<template>
  <div class="tw-tag-slide flex items-center gap-2">每日一文
    <i-ep-CopyDocument class="text-[#000] cursor-pointer" v-copy="time"/>
  </div>
</template>
<script setup lang="ts">
import {getHour} from "@/utils/globaltime";
const time = getHour();
</script>

<style scoped lang="scss">

</style>

效果

End

看似较为复杂难懂的东西只需一个demo即可快速理解上手,这里是小李的李li,若对您有帮助,记得点赞收藏,给个鼓励!公zong号:【小李的李li】

相关推荐
易朵朵2 小时前
CSS 格式化上下文
css·vue.js
雪芽蓝域zzs3 小时前
第6节:远程搜索弹窗(remote 模式),弹窗滚动加载分页
vue.js·elementui
Highcharts.js4 小时前
图表开发实战总结| 7 条“黄金法则”与“性能铁律”
前端·javascript·vue.js·编辑器·时序数据库·highcharts
Wx-bishekaifayuan7 小时前
springboot陨石鉴收系统96265-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spring·课程设计
FYKJ_20108 小时前
springboot家政服务平台66766-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·mysql·spark·课程设计
墨鱼老师8 小时前
Go+Gin+Vue 毕设项目:Gin 框架搭建后端基础接口实战
vue.js·golang·go·gin·前后端分离·计算机毕业设计·go 后端
ym hyd 11117 小时前
试题库管理系统源码 Java+SpringBoot+Vue3 前后分离
java·vue.js·spring boot·毕设
雪芽蓝域zzs1 天前
第4节:el-select 弹窗事件控制(打开 / 关闭、选中事件、弹窗滚动事件)
vue.js·elementui
web打印社区1 天前
HTML5 静默打印:前端页面怎么调用打印机且尽量不弹窗
前端·javascript·vue.js·pdf·html·html5
雪芽蓝域zzs1 天前
第3节:el-select 弹窗定位、滚动穿透与挂载容器(popper-append-to-body /teleported)
vue.js·elementui