Vue 自定义指令

59 阅读1分钟

参考

学习Vue3 第二十七章(自定义指令directive)_vue3的directive_小满zs的博客-CSDN博客

使用演示

参数说明

<template>

    <div v-move:a.b="{background:'red'}"></div>

</template>

<script setup lang='ts'>
import { Directive } from 'vue';
const vMove:Directive = {
    mounted(...args:Array<any>){
        console.log("onMounted")
        console.log(args)
    }
}


</script>
image.png

简单使用

<template>

    <div v-move:a.b="{background:'red'}">  demo </div>

</template>

<script setup lang='ts'>
import { Directive, DirectiveBinding } from 'vue';

type Dir = {
    background:string
}

const vMove:Directive = {
    mounted(el:HTMLElement,dir:DirectiveBinding<Dir>){
        console.log("onMounted")
        console.log(dir.value.background)
        el.style.background = dir.value.background
    }
}

</script>

image.png

相关案例

Vue 图片懒加载 - 掘金 (juejin.cn)

Vue 自定义指令——实现拖拽 - 掘金 (juejin.cn)

Vue 自定义指令——鉴权 - 掘金 (juejin.cn)